Skip waiting for a website timer selenium Python

2019-01-20 17:56发布

I'm running Selenium in Python ide with geckodriver.

The site I'm trying to open has a timer of 30 seconds that after this 30 seconds a button appears and I send a click on it.

What I'm asking is the following: Can I somehow ignore/skip/speed up the waiting time?

Right now what I'm doing is the following:

driver = webdriver.Firefox()
driver.get("SITE_URL")
sleep(30)
driver.find_element_by_id("proceed").click()

Which is very inefficient because every time I run the code to do some tests I need to wait.

Thanks in advance, Avi.

UPDATE: I haven't found a way to get over the obstacle but until I do I'm trying to focus the next achievable progress:

<video class="jw-video jw-reset" disableremoteplayback="" webkit-playsinline="" playsinline="" preload="metadata" src="//SITE.SITE.SITE/SITE/480/213925.mp4?token=jbavPPLqNqkQT1SEUt4crg&amp;time=1525458550" style="object-fit: fill;"></video>

(censored site's name)

In each page there is a video, all the videos are under the class "jw-video jw-reset" I had trouble using find element by class so I used:

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, "video[class='jw-video jw-reset']")))

It works but I can't figure how to select the element's src...

1条回答
迷人小祖宗
2楼-- · 2019-01-20 18:42

As per your code trial you can remove the time.sleep(30) and induce WebDriverWait for the element to be clickable as follows :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
# lines of code
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, "proceed"))).click()

Note : Configure the WebDriverWait instance with the maximum time limit as per your usecase. The expected_conditions method element_to_be_clickable() will return the WebElement as soon as the element is visible and enabled such that you can click it.

查看更多
登录 后发表回答