首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么ActionChains(driver).move_to_element(elem).click().perform()两次?

为什么ActionChains(driver).move_to_element(elem).click().perform()两次?
EN

Stack Overflow用户
提问于 2016-10-21 07:36:38
回答 1查看 7.1K关注 0票数 3

我试图通过"http://weixin.sogou.com/“抓取微信的公共账户中的关键词。

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

谁能告诉我为什么和如何解决!谢谢!

源代码如下,很抱歉评论是中文的。

代码语言:javascript
复制
# 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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-10-21 08:07:11

你可以连锁你的动作,所以不需要在每个动作之后执行。

代码语言:javascript
复制
actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage)
actions.perform()

代码语言:javascript
复制
actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage).perform()
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40170915

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档