如何停止python selenium webdriver无限页面加载

时间:2019-09-11 12:41:04

标签: python selenium webdriver python-3.6

我正在尝试加载网址 https://www.eurexchange.com/exchange-en/products/idx/stx/blc/EURO-STOXX-50-Index-Options-46548

但是某些页面最近会无限加载,并且带有selenium.webdriver.execute_script('window.stop()')的time.sleep()函数不能解决问题。

尝试:

browser.execute_script('window.stip()')
and browser.find_element_by_tag_name("body").sendkeys(Keys.ESCAPE)

仍然看到页面加载。

website_url = 'https://www.eurexchange.com/exchange-en/products/idx/stx/blc/EURO-STOXX-50-Index-Options-46548'
browser = webdriver.Chrome(executable_path=chromedriver_path, options=chrome_options)
dropdown = Select(browser.find_element_by_xpath('//*[@id="maturityDate"]'))
dropdown_len = len(dropdown.options)

for i in range(1, dropdown_len):
    rows = []
    time.sleep(2)
    browser.execute_script('window.stop()')
    # browser.find_element_by_tag_name("body").sendkeys(Keys.ESCAPE)

2 个答案:

答案 0 :(得分:0)

我相信您正在寻找Selenium's Python documentation,这使您可以指定一个超时等待元素出现在页面中,如果该元素在分配的时间内没有出现,则可以取消脚本或您希望执行的任何操作。

该示例来自{{3}}

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

driver = webdriver.Firefox()
driver.get("http://somedomain/url_that_delays_loading")
try:
    element = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.ID, "myDynamicElement"))
    )
finally:
    driver.quit()

答案 1 :(得分:0)

您可以将超时用于永远的等待

browser.implicitly_wait(60) # 60 seconds to wait && this is only used with chrome
browser.set_page_load_timeout(60) # for other browsers

这将引发异常,因此您需要尝试以下语句,例如:

browser.implicitly_wait(60)

try:
    browser.get('http://www.example.com')
except:
    print "loading exceeded the time limit" # not sure what python version you are using