Selenium在循环中的所有元素上移动\u到\u元素

2024-09-27 09:32:00 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经编写了一段代码,可以登录LinkedIn,进入sales navigator页面,并遍历每个profile元素

问题出现在以下几行中(确切地说是action.move_to_element(element.perform())。“元素”包含配置文件

elements = driver.find_elements_by_xpath("//ol[@class='search-results__result-list']/li")
for element in elements:
    action.move_to_element(element).perform()

action.move\u to\u元素并不是它应该去的地方。i、 e对于第四个元素,它将逐个移动到前四个元素上,而不是移动到第四个元素。同样,对于第五个元素,它将逐个移动到前五个元素上。对其他人来说也是如此

这是完整的代码

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time
driver = webdriver.Chrome('C:\\Program Files (x86)\\chromedriver.exe')

driver.get('https://www.linkedin.com/uas/login')
elementID = driver.find_element_by_id('username')
elementID.send_keys('')
elementID = driver.find_element_by_id('password')
elementID.send_keys('')
elementID.submit()

url = 'https://www.linkedin.com/sales/search/people?doFetchHeroCard=false&geoIncluded=103644278&logHistory=true&page=1&rsLogId=757633298&searchSessionId=5LvWxKggSSC4Ozevq4%2BRCg%3D%3D&titleIncluded=%2522Real%2520Estate%2520Agent%2522%2520NOT%2520%2522%2520Licensed%2522&titleTimeScope=CURRENT'
driver.get(url)
time.sleep(3)

action =  ActionChains(driver)
elements = driver.find_elements_by_xpath("//ol[@class='search-results__result-list']/li")
for element in elements:
    action.move_to_element(element).perform()

Tags: to代码import元素searchmovebydriver
1条回答
网友
1楼 · 发布于 2024-09-27 09:32:00

我认为这是因为您正在向ActionChain添加一个新的move\u to\u元素动作,然后在每次迭代中执行整个链。您需要在循环结束后立即执行()所有操作(或每次创建一个新的ActionChain)

for element in elements:
    action.move_to_element(element)
action.perform()

相关问题 更多 >

    热门问题