我想点击连续的链接,以便在Job Posting page
上找到company's domains
,WebDriver使用Python。
然而,WebDriver
只是一次又一次地点击第一个链接。
代码:
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()
它之所以单击第一个链接是因为这一行。
driver.find_element_by_class_name('job-board-job-title').click()
这将始终单击第一个元素。
您需要使用索引。请尝试以下代码。
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()