Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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[@class="ui-chat__messageheader"]/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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

 txt=i.find_element(By.XPATH,'//div[@class="ui-chat__messageheader"]/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[@class="ui-chat__messageheader"]/time').get_attribute('title')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading