2014-09-19 101 views
0

我正在編寫一個殭屍程序,它將遵循www.quora.com上的用戶。以下是我使用的代碼部分在那裏我得到超時異常:如何處理python中的超時異常

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
import time 
import urllib 

driver = webdriver.Firefox() 
driver.get('http://www.quora.com/') 
time.sleep(10) 

wait = WebDriverWait(driver, 10) 

form = driver.find_element_by_class_name('regular_login') 
time.sleep(10) 
#add explicit wait 

username = form.find_element_by_name('email') 
time.sleep(10) 
#add explicit wait 

username.send_keys('[email protected]') 
time.sleep(30) 
#add explicit wait 

password = form.find_element_by_name('password') 
time.sleep(30) 
#add explicit wait 

password.send_keys('def') 
time.sleep(30) 
#add explicit wait 

password.send_keys(Keys.RETURN) 
time.sleep(30) 

#search = driver.find_element_by_name('search_input') 
search = wait.until(EC.presence_of_element_located((By.XPATH, "//form[@name='search_form']//input[@name='search_input']"))) 

search.clear() 
search.send_keys('Kevin Rose') 
search.send_keys(Keys.RETURN) 

link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Kevin Rose"))) 
link.click() 
#Wait till the element is loaded (Asynchronusly loaded webpage) 

handle = driver.window_handles 
driver.switch_to.window(handle[1]) 
#switch to new window 

element = WebDriverWait(driver, 2).until(EC.presence_of_element_located((By.PARTIAL_LINK_TEXT, "Followers"))) 
time.sleep(30) 
element.click() 
#goes to Kevin Rose followers page 
time.sleep(30) 

button = driver.find_elements_by_xpath("//a[contains(text(), 'Follow')]") 
#Locate follow button on the page 
no_of_followers = len(button) 
#total number of unfollowed users 
print no_of_followers 


    while(no_of_followers > 0): 
    # execute only if there are unfollowed users on page 

     count = 1 

     while(count < no_of_followers): 

      time.sleep(30) 
      link = wait.until(EC.presence_of_element_located((By.LINK_TEXT, "Follow"))) 
      time.sleep(30) 
      link.click() 
      time.sleep(30) 
      print count 
      count = count + 1 


     time.sleep(30) 
     driver.execute_script("window.scrollTo(0, document.body.scrollHeight);") 
     time.sleep(30) 
     button = driver.find_elements_by_xpath("//a[contains(text(), 'Follow')]") 
     time.sleep(30) 
     no_of_followers = len(button) 

執行的代碼後,我得到在成功執行一次之後內環「TimeoutException異常」的錯誤。

我該如何解決這個問題?

回溯:

回溯(最近最後調用):文件 「C:\ Python27 \ quorabot7」, 線72,在 鏈路= wait.until(EC.presence_of_element_located((By.LINK_TEXT ,「Follow」))) 「C:\ Python27 \ lib \ site-packages \ selenium \ webdriver \ support \ wait.py」

+0

請包含完整的Traceback。 – dano 2014-09-19 17:24:09

+0

另外,請發佈完整的代碼,以便我們可以複製它並逐步進行調試。謝謝。 – alecxe 2014-09-19 17:26:08

+0

發佈完整的代碼和追溯。 – Siddhesh 2014-09-19 17:29:10

回答

3

你會得到一個TimeoutException becaus e Selenium在您設定的等待時間內找不到該元素。這意味着您的定位器策略不正確。

我還沒有測試過你的其他定位器,但如果它確實是失敗的內循環...我的解決方案如下。

通過凱文·哈特的頁面上的DOM看後,我可以看到你感興趣的按鈕:

<a class="follow_button with_count" href="#" action_click="UserFollow" id="__w2_Mab4s9V_follow_user">Follow<span class="count">43.8k</span></a> 

你應該試試這個:

link = wait.until(EC.presence_of_element_located(\ 
     (By.className, "follow_button with_count"))) 

或本:

link = wait.until(EC.presence_of_element_located(\ 
     (By.XPATH, '//a[@action_click="UserFollow"]'))) 
+0

謝謝。有效。但是,你能否解釋爲什麼「link = wait.until(EC.presence_of_element_located((By.LINK_TEXT,」Follow「)」)失敗? – Siddhesh 2014-09-20 17:40:41

+0

這可能與LINK_TEXT定位器策略和你感興趣的按鈕有關。我不太熟悉LINK_TEXT – sheeptest 2014-09-21 02:35:36

+0

啊,實際上這可能是因爲span是在'a'標籤的內部,所以它確實是「Follow 49.7k」 d需要給出LINK_TEXT的值。 – sheeptest 2014-09-21 02:38:15

0

正確的是By.CLASS_NAME,而不是By.ClassName