【发布时间】:2014-09-19 17:17:58
【问题描述】:
我正在编写一个机器人来跟踪 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)
执行代码后,我在成功执行一次后内部循环中出现“TimeoutException”错误。
我该如何解决这个问题?
追溯:
Traceback(最近一次调用最后一次):文件“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", 第 71 行,直到 raise TimeoutException(message) TimeoutException: Message: ''
【问题讨论】:
-
请包含完整的 Traceback。
-
另外,请将完整的代码贴出来,以便我们逐步复现和调试。谢谢。
-
发布了完整的代码和回溯。
标签: python python-2.7 selenium-webdriver web-scraping bots