为什么ActionChains(驱动程序).move_to_element(elem).click()。perform()两次

时间:2016-10-21 07:36:38

标签: python selenium

我尝试抓取微信公众号,包括关键词" 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

1 个答案:

答案 0 :(得分:1)

您可以链接您的行动,因此无需在每次行动后执行。

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()