Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ActionChains(driver).move_to_element(elem).click().perform() twice

I try to crawl the wechat public accounts includes the key word through http://weixin.sogou.com

But i find i must use twice ActionChains(driver).move_to_element(nextpage).click().perform(),it can still work,and go to the next page !

who can tell me why and how to fix ! Thank you!

The source code are as follow , and sorry the comments are in the Chinese .

# 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
like image 441
nghuyong Avatar asked Oct 17 '25 06:10

nghuyong


1 Answers

You can chain your action, so no need to do perform after each action.

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

OR

actions = ActionChains(driver)
actions.move_to_element(nextpage)
actions.click(nextpage).perform()
like image 65
saurabh baid Avatar answered Oct 20 '25 17:10

saurabh baid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!