gpt4 book ai didi

python - 为什么 ActionChains(driver).move_to_element(elem).click().perform() 两次

转载 作者:行者123 更新时间:2023-12-01 03:37:28 25 4
gpt4 key购买 nike

我尝试通过http://weixin.sogou.com抓取包含关键词的微信公众号

但是我发现我必须使用两次 ActionChains(driver).move_to_element(nextpage).click().perform(),它仍然可以工作,然后转到下一页!

谁能告诉我原因以及如何解决!谢谢!

源代码如下,抱歉注释是中文的。

# coding=utf-8
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

key = u"江南大学" #搜索的关键词
driver = webdriver.Chrome()
driver.get("http://weixin.sogou.com/")
assert u'搜狗微信' in driver.title
elem = driver.find_element_by_id("upquery")
elem.clear()
elem.send_keys(key)
button = driver.find_element_by_class_name("swz2") #搜索公众号
button.click()
WebDriverWait(driver,10).until(
EC.title_contains(key)
)
count = 0
while True:
for i in range(10):
try:
wechat_name = driver.find_element_by_xpath("//*[@id=\"sogou_vr_11002301_box_{}\"]/div[2]/h3".format(i)).text
print wechat_name
wechat_id = driver.find_element_by_xpath("//*[@id=\"sogou_vr_11002301_box_{}\"]/div[2]/h4/span/label".format(i)).text
print wechat_id
wechat_intro = driver.find_element_by_xpath("//*[@id=\"sogou_vr_11002301_box_{}\"]/div[2]/p[1]/span[2]".format(i)).text
print wechat_intro
print "*************************"
count += 1
except:
pass
try:
nextpage = driver.find_element_by_xpath("//*[@id=\"sogou_next\"]") #下一页的按钮
actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click().
actions.perform()
actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click().
actions.perform()
except Exception,e:
print e
break
driver.quit()
print count

最佳答案

您可以链接您的操作,因此无需在每个操作后执行。

actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage)
actions.perform()

actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage).perform()

关于python - 为什么 ActionChains(driver).move_to_element(elem).click().perform() 两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40170915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com