Home > Mobile >  How to click on element that contains text from list?
How to click on element that contains text from list?

Time:01-25

I'm trying to click on element by xpath that contains text from list.

categories = (WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, 'div.sph-MarketGroupNavBarButton '))))
categorylist = []

for category in categories:
    text = category.text
    categorylist.append(text)
    print(text)

categoryindex = categories[3]
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()
print(categoryindex)

When I run it, the script creates the list and prints out list content and then I want to search for element that contains the text from the list and click on it, but this part of code doesn't work : It doesn't click or print the specified text. Nothing happens and I get timeout error.

categoryindex = categories[3]
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, f"//div[text() ='{categoryindex}']"))).click()
print(categoryindex)

When I replace {categoryindex} with actual text from list it clicks on it.

CodePudding user response:

Change

categoryindex = categories[3]

to

categoryindex = categorylist[3]
  • Related