[英]Make selenium click Consecutive Elements with same class for n times
I have a page with 25 Companies located in same class. 我的页面上有25个位于同一班级的公司。 Here is the link for the website - This is the HTML code:
这是网站的链接-这是HTML代码:
<section class="rslwrp">
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
<section class="jbbg">..</section>
******and so on******
I am not sure, how I make selenium click one class and then browser.back() then move to second then again browser.back()
and then to third and so on. 我不确定如何使硒形成一个类,然后单击browser.back(),然后移至第二个类,然后再次移至
browser.back()
,然后移至第三类,依此类推。 For n number of times. 连续n次。
I am using, 我在用,
browser.find_element_by_xpath('//section[@class="jbbg"]/section[2]/section[1]/aside[1]/p[1]/span/a').click()
Could someone please advise. 有人可以建议。 Thanks in advance for your help.
在此先感谢您的帮助。
Loop over the results of find_elements_by_xpath()
, get all links and get()
them one by one: 循环
find_elements_by_xpath()
的结果,获取所有链接,并一一get()
它们:
links = [link.get_attribute('href') for link in browser.find_elements_by_xpath('//span[@class="jcn"]/a')]
for link in links:
browser.get(link)
Simplest thing to do is to use find_elements_by_xpath
, then iterate through the list: 最简单的方法是使用
find_elements_by_xpath
,然后遍历列表:
linksCount = len(browser.find_elements_by_xpath('//section[@class="jbbg"]/section[2]/section[1]/aside[1]/p[1]/span/a'))
for x in range(linksCount)
browser.find_element_by_xpath('(//section[@class="jbbg"]/section[2]/section[1]/aside[1]/p[1]/span/a)[' + x + ']')
As the previous poster mentioned, you should iterate through each of them for clicking. 如前所述,您应该遍历每个海报以便单击。 However, inside the loop, you have to identify the application back button or browser back button to go back to previous page.
但是,在循环内部,您必须标识应用程序后退按钮或浏览器后退按钮才能返回上一页。
But, note once you gone back to the previous page, the element you grabbed with find_elements may not be valid or stale due to html reload. 但是,请注意,一旦返回上一页,由于html重新加载,用find_elements捕获的元素可能无效或陈旧。 So, you might want to re locate the same element again inside the loop for the program to work
因此,您可能希望再次在循环内重新定位相同的元素,以使程序正常工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.