0

I'm trying to iterate through this list and select the element, can't figure it out.

Code trials:

butonlista = self.wait.until(EC.element_to_be_clickable((By.XPATH, "//input[@id='P17_OWNER01_EMAIL_AD']")))
butonlista.click()
search_result = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, "(//ul[@role='listbox'])[1]")))
print(len(search_result))
for result in search_result:
    if "bacau@otpbank.ro" in result.text:
        result.click()
        break

But print(len(search_result)) returns 1 element, what am I doing wrong?

Snapshot of the HTML:

enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Clswtz
  • 11
  • 1
  • You're getting the parent element. There's only one parent element. Make an xpath for the child elements (maybe by adding li at the end). You want your Xpath to mark all the li elements below the current one – dot Feb 08 '23 at 19:25

1 Answers1

0

Given the HTML, bacau@otpbank.ro is the value of the data-id attribute of the first descendant <li> of it's parent <ul role="listbox" ...>


Solution

To iterate through the all the <li> elements and click on the desired element you can use the following locator strategies:

search_result = self.wait.until(EC.presence_of_all_elements_located((By.XPATH, "//ul[@class='a-IconList' and @aria-labelledby='il1_info']//li")))
print(len(search_result))
for result in search_result:
    if "bacau@otpbank.ro" in result.get_attribute("data-id"):
        result.click()
        break
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352