2017-04-05 90 views
0

HI我試圖暫停執行Selenium幾秒鐘等待Modal彈出窗口顯示。 但time.sleep(5)沒有使用phantomJS工作(我聽說PhantomJS不支持睡眠)。 所以我來了setTimeout。setTimeout在python中不起作用Selenium執行腳本

driver.execute_script('setTimeout(function(){"scroll(0, 300);"}, 3600);') 

但即使在Chrome Selenium驅動程序中它也不起作用。

儘管driver.execute_script('scroll(0, 300);')有效,但我不知道如何在硒中執行setTimeout。

+0

請閱讀以下http:// stackoverfl ow.com/questions/17533024/how-to-set-selenium-python-webdriver-default-timeout –

+0

爲什麼不只是'import time'並在driver.execute_script('scroll()之前設置'time.sleep(3.6)' 0,300);')'? – Andersson

+0

@安德森我試過了。但我不能使用time.sleep(3.6)。我需要等待彈出窗口顯示。當我使用_time.sleep(3)_時,它引發'**元素當前不可交互並且不可操作**'錯誤。當我屏幕截圖時,模態彈出窗口不顯示。 – dizwe

回答

0

如果您需要滿足一些特定的條件下,你可以使用ExplicitWait + ExpectedConditions,如:

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

wait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "Specify css selector for your modal div"))) 
driver.execute_script('scroll(0, 300);') 

* 你可以用你喜歡By.XPATHBy.ID任何選擇,等

這代碼應允許您等待(最多10秒),直到執行腳本之前所需元素的可見性爲止

+0

我試過了你提到的方式。但是當我得到截圖時,模態div沒有出來。 (驅動程序,10).until(EC.visibility_of_element_located((By.XPATH,「// * [@ id = \」teacher_booked_modal \「]/div/div/div」))'' – dizwe