2016-10-21 4 views
0

私は「http://weixin.sogou.com/なぜActionChains(ドライバ).move_to_element(ELEM).click()。(実行)を2回

を通じてキーワードが含まれて微信公共のアカウントをクロールしてみてください。しかし、私は二回を使用する必要があります見つけます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 
+0

私は答えを見つける:。 'action.move_to_element(NEXTPAGE).perform'first、その後' action.clickを()(実行します) '。それは動作します! – nghuyong

答えて

0

アクションを連鎖させることができるため、アクションごとに実行する必要はありません。

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

いいえ、動作しません! – nghuyong

+0

試してみると、2回使用する必要があります。 – nghuyong

+0

テスト後に回答を投稿しました。また、こちらのドキュメントを確認してください。 https://seleniumhq.github.io/selenium/docs/api/py/webdriver/selenium webdriver.common.action_chains.html –

関連する問題