如何返回符合驱动程序.driver.find_element_by_partial_link_text条件的最后一个元素?

2024-04-27 02:18:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个文档,可以使用selenium/python命令driver.find_element_by_partial_link_text找到一个或两个链接。不管是最后一个还是最后两个,我都要返回。在

有办法吗?在

也就是说,页面有时有文本链接stackoverflow.com/xxxxxxx以及stackoverflow.com/iwantthislink. 其他时候只是stackoverflow.com/iwantthislink. 在

当我使用driver.find_element_by_partial_link_text("stackoverflow.com/")时,它返回stackoverflow.com/xxxxxxx因为它早就来了stackoverflow.com/iwantthislink. 在

我只需要一种方法来始终返回列表中的最后一个元素。在


Tags: text文档命令comby链接driverselenium
1条回答
网友
1楼 · 发布于 2024-04-27 02:18:08

find_elements_by_partial_link_text返回一个列表。(注意elementselement。)

要访问最后一个元素,请执行以下操作:

my_elem = driver.find_elements_by_partial_link_text('sometext')[-1]

更好的是,事先测试元素的存在:

^{pr2}$

相关问题 更多 >