硒拉动相同的“ li”标记“ ul”而不是所有人

发布于 2025-02-08 14:10:25 字数 1259 浏览 1 评论 0原文

我的代码目前看起来如此:

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)

对我的理解,“ lis”应该在“ the_ul”中具有每个独特的“ li”。但是,打印语句如下:

[<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

“ lis”中的所有元素似乎与打印语句相同的输出的值完全相同。我可以在给定的“ li”标签中确认所有值都应不同。

我不确定为什么会这样,我尝试更改lis = the_ul.find_elements(by.tag_name,'li') to lis = the_ul.find_elements(by.tag_name) ,'// li'),但仍然没有解决问题。也许我没有正确迭代这些元素?但是我似乎找不到这个问题。

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

若言繁花未落 2025-02-15 14:10:25
txt=i.find_element(By.XPATH,'//div[@class="ui-chat__messageheader"]/time').get_attribute('title')

由于元素的X path量如何工作,可能需要是以下内容。需要.//。

txt=i.find_element(By.XPATH,'.//div[@class="ui-chat__messageheader"]/time').get_attribute('title')
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')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文