[Python] Is it possible to add a custom element locator strategy to Selenium (Python)

Discussão em 'Python' iniciado por Stack, Fevereiro 15, 2025.

  1. Stack

    Stack Membro Participativo

    I am writing a test and would like to replicate the user's workflow -- users don't go by CSS selectors, element names, or XPath queries, all they can see is what the user agent renders. To that end, I would like to find an element on the page based on its [rendered] content, and being case-insensitive at that (since that's what more closely resembles how the user will approach it). I'd accept the equivalent of the //label[contains(lower-case(text()), "username")] XPath 2.0 expression.

    Since Selenium appears to defer XPath query execution to the driven browser, where my testing indicates it's only ever XPath 1.0, I can't use XPath 2.0 functions like lower-case or matches, in order to find an element that has the desired content.

    None of the other locator strategies (e.g. using a CSS selector) match capabilities of XPath, certainly none of them allow me to find an element based on its content.

    I thought if it were possible to write a custom locator strategy somehow, that would allow me to use it with the find_element procedure, but I can't find much information on whether that is even possible, let alone if it's possible with specifically the selenium Python module provided by Selenium, or how to accomplish it in the first place.

    Is it possible? How can it be done, if so? Alternatively, am I solving the wrong problem?

    Continue reading...

Compartilhe esta Página