Python Selenium - 如何循环到最后一个 <li>站点中的元素 [英] Python Selenium - How to loop to the last <li> element in a site
问题描述
我创建了一个 python selenium 脚本,该脚本应该浏览网站并收集人员个人资料 (https://www.shearman.com/people).该程序不会遍历页面来收集链接.我用过这个不起作用;
I have created a python selenium script that should navigate through a website and collect people profiles (https://www.shearman.com/people). The program won't loop through the pages to collect the links. I have used this which doesn't work;
try:
# this is navigate to next page
driver.find_element_by_xpath('//div[@id="searchResultsSection"]/ul/li[12]').click()
time.sleep(1)
except NoSuchElementException:
break
next 按钮背后的语法如下所示;
The syntax behind the next button can be seen below;
<a href="" onclick="PageRequest('2', event)" class="xh-highlight">></a>
有人不知道如何编写点击下一步按钮的代码吗?
Does anybody no how to write the code to click the next button?
推荐答案
如果你看HTML元素的图片为>
,它是
标签的最后一个 标签内的
标签.因此,要在其上调用
click()
,您可以使用以下代码块:
If you look at the HTML of the element with image as >
, it is the <a>
tag within the last <li>
tag of the <ul>
tag. So to invoke click()
on it you can use the following code block :
driver.find_element_by_xpath("//ul[@class='results-pagination']/li[last()]/a").click()
这篇关于Python Selenium - 如何循环到最后一个 <li>站点中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!