2016-11-20 241 views
1

我正在爲Facebook報廢創建一個腳本。 我在Ubuntu 14.04.5 LTS下使用Selenium 2.53.6,Gecko驅動程序0.11.1和Firefox 50。我也嘗試過在Windows 10下使用Gecko以及Chromedriver,同樣的結果(我將在下面描述):(Selenium WebDriverWait在網絡報廢時返回Python錯誤

下面的代碼片段我使用我從Explicitly Waits的原始文檔複製的:

import datetime, time, sys, argparse 
from time import strftime 
from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary 
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0 
from selenium.webdriver.support import expected_conditions as EC# available since 2.26.0 
... 
... 
def main(usrEmail, pwd, numberOfScrolls, secondsWait, searchItem, outFilename): 
    # Open Firefox Browser 
    binary = FirefoxBinary('/usr/bin/firefox') 
    browser = webdriver.Firefox(firefox_binary=binary) 

    #put browser in specific position 
    browser.set_window_position(400, 0) 

    # goto facebook 
    browser.get("http://www.facebook.com") 

    #waiting(5) 
    try: 
     element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "u_0_p"))) 
    finally: 
     logging("logging in...") 

的問題是,我得到這個錯誤:

File "fb.py", line 86, in main 
    element = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, "u_0_p"))) 
NameError: name 'By' is not defined 

由於我只使用的形式time.sleep(delay)延遲和程序的工作原理相當不錯的解決方法,但如果我會更好有WebDriverWait功能。

我是否錯過了某些明顯的東西,或者它是一種硒缺陷或網絡瀏覽器功能? 任何幫助/澄清將不勝感激!

感謝您的時間!

+1

我有類似的問題。從我收集的信息來看,並非所有WebDrivers都支持Explicitely.Wait功能。 – Fang

回答

1

可能是你缺少的By定義:

from selenium.webdriver.common.by import By 
+0

那麼......尷尬(顯而易見)的解決方案對於每一個新的編程挑戰都很常見! :-) 非常感謝你@Moshisho ... 誤導我的是什麼,基本上是原始文檔,跳過這個...細節! –

+0

很高興幫助! ;) – Moshisho

+0

如果我的答案已解決您的問題,接受它將非常感謝! – Moshisho