简体   繁体   English

使硒单击相同类的连续元素n次

[英]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.

相关问题 在Python中使用Selenium单击具有相同类名的所有元素 - Using Selenium in Python to click through all elements with the same class name 如何通过 lambda function 从列表中创建 n 个连续元素? - How to make n consecutive elements from list by lambda function? 同时点击多个元素 selenium Python - Click multiple elements at the same time selenium Python 使用 Selenium 我试图单击页面上具有相同类的所有元素,但它不起作用 - Using Selenium I am trying to click all elements on the page with the same class but it is not working 当有多个相同的 class 并且没有使用 Python 的 id 元素时,使用 selenium 创建点击事件 - Create a click event using selenium when there are multiple same class and no id elements using Python 同时点击同一个class的所有元素 - Click on all elements of the same class at the same time 如何在硒中找到同类元素的位置 - How to find position of elements with same class in selenium Selenium 在迭代期间跳过相同 class 的元素 - Selenium skips elements of the same class during iteration 如何使用for循环点击selenium中具有相同XPATH的多个元素? - How to click through multiple elements with same XPATH in selenium using for loop? 生成具有 n 个连续重复元素的排列 - Generating permutations with n consecutive repeated elements
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM