skip to Main Content

I’m trying to write a function, which returns a list of elements from any level of nesting from the html, containing the word ‘products’ in its text, case insensitive.

here is my function:


def __find_elements(driver):
    return driver.find_elements(By.XPATH,
                                "//*[contains(translate(descendant::text(), "
                                "'ABCDEFGHIJKLMNOPQRSTUVWXYZ', "
                                "'abcdefghijklmnopqrstuvwxyz'), "
                                "'products')]")

;
for some reason, processing this part of HTML


<a class="nav-link main-navigation-link" href="https://www.leuze.com/en-int/products" itemprop="url" data-flyout-menu-trigger="e3e2319128d6464d8a025ac00823ce4e" title="Products">
    <div class="main-navigation-link-text">
        <span itemprop="name">Products</span>
    </div>
</a>

,
the function returns [] instead of the list, containing the following span element:


<span itemprop="name">Products</span>

.

QUESTION:
Could you please clarify, how to modify the function to return a list with the mentioned above span element, while processing the part of HTML?

Expected:
list with the element

<span itemprop="name">Products</span>

Actually happened:
empty list

2

Answers


  1. Try this :

    elements = driver.find_elements_by_xpath("//span[@itemprop='name']")
    

    this will get all the elements within a span that have itemprop=’name’

    Login or Signup to reply.
  2. If it were me, I would change the XPath to

    //*[text()='Products']
    

    That will get you any element that contains the text, "Products". What other capitalization are you expecting? All caps or all lower case? If those are truly alternatives, I would change it to

    //*[text()='products'] | //*[text()='Products'] | //*[text()='PRODUCTS']
    

    or some variation only containing the spellings I was expecting. I think it’s much more readable but that’s just my opinion.

    If those still don’t work, you are likely running into a timing issue and you need to add a WebDriverWait or it may be that some/all of the found elements are not visible so their text value will be returned as empty string.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search