2017-07-27 62 views
0

我有這樣的代碼,它應該保持了一段時間(5秒)後,秒錶添加到我的劇本超時硒

from selenium import webdriver 
from selenium.webdriver.common.keys import Keys 
import time 

start = time.clock() 

driver = webdriver.Firefox() 
driver.get('https://ebay.com') 

,比其他工作人員,像這樣:

element = driver.find_element_by_id("username") 
element.send_keys("some text") 

怎麼做?

+0

然後,您需要使用[等待](http://selenium-python.readthedocs.io/waits.html#waits),它將一直等到元素出現。 –

+0

@SaurabhGaur我發現「直到」。我可以使用它,直到出現element_by_id(「用戶名」)。你能告訴我該怎麼做,請 –

+0

圍繞它有很多例子,請按照[this](https://stackoverflow.com/questions/26566799/selenium-python-how-to-wait-until-the-page- is-loaded)或[this](https://stackoverflow.com/questions/32717334/wait-until-element-is-not-present)..謝謝 –

回答

1

我花了很多時間,在上帝和來自StackOverflow的許多人的幫助下,我終於解決了它。

from selenium import webdriver 
from selenium.webdriver.support.ui import WebDriverWait 
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By 
from selenium.common.exceptions import TimeoutException 

driver = webdriver.Firefox() 
driver.get(''https://ebay.com'') 
delay = 30 

try: 
    myElem = WebDriverWait(driver, delay).until(EC.presence_of_element_located((By.ID, 'username'))) 
    element = driver.find_element_by_id("username") 
    element.send_keys("some text") 
except TimeoutException: 
    print ("Loading took too much time!") 

這將是最好的,在我看來。