简体   繁体   English

如何在 Selenium 中的 web 元素列表上迭代点击?

[英]How do I iterate clicks over a list of web elements in Selenium?

I'd like to click on consecutive links in order to find the company's domains on a Job Posting page , with WebDriver using Python.我想单击连续链接以在Job Posting page上找到company's domains ,WebDriver 使用 Python。

However, the WebDriver clicks on the first link only over and over again.但是, WebDriver只会一遍又一遍地点击第一个链接。

Code :代码

import pandas as pd
from selenium import webdriver
from time import sleep
driver = webdriver.Chrome('/Users/user/Downloads/chromedriver_win32/chromedriver')
driver.get('https://dribbble.com/jobs')
assert 'Dribbble' in driver.title
total_web = []
quotes = len(driver.find_elements_by_class_name("job-board-job-list"))
for quote in range(quotes): 
        driver.find_element_by_class_name('job-board-job-title').click()
        for a in driver.find_elements_by_xpath("//*[@id='content']/div[3]/div[1]/div[1]/div/ul/li/a"):
            website = a.get_attribute('href')

        new1 = ((website))
        total_web.append(new1)
        driver.back()

        quote+=1

df_web = pd.DataFrame(total_web,columns=['website'])
driver.close()

The reason it is clicking 1st link because of this line.由于这条线,它单击第一个链接的原因。

driver.find_element_by_class_name('job-board-job-title').click()

This will always click the first element.这将始终单击第一个元素。

You need to use indexing.Try below code.您需要使用索引。试试下面的代码。

elements=driver.find_elements_by_class_name("job-board-job-list")
quotes = len(elements)
for quote in range(quotes):
    #Added this line to avoid stale which re-assigned the element again.
    elements = driver.find_elements_by_class_name("job-board-job-list")
    elements[quote].click()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM