如何在python中处理超时异常

时间:2014-09-19 17:17:58

标签: python python-2.7 selenium-webdriver web-scraping bots

我正在编写一个可以关注www.quora.com上用户的机器人。以下是我正在使用的代码的一部分,我得到超时异常:

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('abc@gmail.com')
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)

执行代码后我得到&#34; TimeoutException&#34;成功执行一次后内部循环出错。

我该如何解决这个问题?

回溯:

  

回溯(最近一次调用最后一次):文件&#34; C:\ Python27 \ quorabot7&#34;,   第72行,在       link = wait.until(EC.presence_of_element_located((By.LINK_TEXT,&#34; Follow&#34;)))文件   &#34; C:\ Python27 \ lib中\站点包\硒\的webdriver \支持\ wait.py&#34 ;,   第71行,直到       raise TimeoutException(message)TimeoutException:Message:&#39;&#39;

2 个答案:

答案 0 :(得分:3)

您收到TimeoutException,因为Selenium在您设置为等待的时间内找不到该元素。这意味着您的定位器策略不正确。

我没有测试过你的其他定位器,但是如果真的是内部循环失败了......我的解决方案就在下面。

在Kevin Hart的页面上查看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"]')))

答案 1 :(得分:0)

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