Home > Enterprise >  Selenium pulling the same "li" tag in "ul" instead of all of them
Selenium pulling the same "li" tag in "ul" instead of all of them

Time:06-18

My code currently looks as such:

the_ul =driver.find_element(By.XPATH,'//ul[@aria-label="Chat content"]')
    lis =the_ul.find_elements(By.TAG_NAME,'li')
    print(lis)
    print("this is len lis: ",len(lis))
    for i in lis:
        txt=i.find_element(By.XPATH,'//div[@]/time').get_attribute('title')
        print("TXT: ",txt)

to my understanding 'lis' should have every unique 'li' within 'the_ul'. However, the print statements are as follows:

[<selenium.webdriver.remote.webelement.WebElement (session="d8381099435e4dd6d9b64c380a16325f", element="e1b79379-bb3d-4a7a-aa83-16d9b65be830")>, <selenium.webdriver.remote.webelement.WebElement (session="d8381099435e4dd6d9b64c380a16325f", element="671d3f30-f88d-45c1-b478-a343833b6334")>, <selenium.webdriver.remote.webelement.WebElement (session="d8381099435e4dd6d9b64c380a16325f", element="dab6680b-fcf7-410f-9b08-71af744bc6c8")>]
this is len lis:  3
TXT:  June 13, 2022 6:35 PM
TXT:  June 13, 2022 6:35 PM
TXT:  June 13, 2022 6:35 PM

It seems like all the elements in the 'lis' are the same as such the print statement outputs the exact same value. I can confirm all the values should be different within the given 'li' tags.

I am unsure of why this seems to be the case, I had tried changing lis =the_ul.find_elements(By.TAG_NAME,'li') to lis =the_ul.find_elements(By.TAG_NAME,'//li') but still, it does not fix the issue. Perhaps I am not iterating over the elements correctly? But I can't seem to find the issue.

CodePudding user response:

 txt=i.find_element(By.XPATH,'//div[@]/time').get_attribute('title')

Might need to be the following due to how xpathing from an element works. Which needs .// .

 txt=i.find_element(By.XPATH,'.//div[@]/time').get_attribute('title')
  • Related