Python Selenium - How to loop to the last <li> element in a site

左心房为你撑大大i 提交于 2020-01-30 13:04:34

问题


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

The syntax behind the next button can be seen below;

<a href="" onclick="PageRequest('2', event)" class="xh-highlight">&gt;</a>

Does anybody no how to write the code to click the next button?


回答1:


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()



回答2:


You can try to use below line to click Next button

driver.find_element_by_link_text(">").click()



回答3:


you can do it yourself :

  • ctrl+shift+i
  • right click on the next button -> inspect
  • right click on the next button code -> copy -> copy selector

then you store the button in a WebElement nextButton then

nextButton.click();


来源:https://stackoverflow.com/questions/49937997/python-selenium-how-to-loop-to-the-last-li-element-in-a-site

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!