Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
113 views
in Technique[技术] by (71.8m points)

python - What does Selenium .set_script_timeout(n) do and how is it different from driver.set_page_load_timeout(n)?

In context of python selenium, I don't quite understand the exact difference of driver.set_page_load_timeout(n) VS. driver.set_script_timeout(n). Both seem to be used interchangeable to set a timeout to load an URL via driver.get(URL), but sometimes also together.

Scenario 1:

driver.set_page_load_timeout(5)
website = driver.get(URL)
results = do_magic(driver, URL)

Scenario 2:

driver.set_script_timeout(5)
website = driver.get(URL)
results = do_magic(driver, URL)

How do both scenarios differ? Which situations trigger a timeout in one but not the other scenario?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per the Selenium-Python API Docs set_page_load_timeout(n) and set_script_timeout(n) both are timeout methods which are used to configure the webdriver instance to abide by during the program execution.

set_page_load_timeout(time_to_wait)

set_page_load_timeout(time_to_wait) sets the amount of time to wait for a page load to complete before throwing an error and is defined as :

    def set_page_load_timeout(self, time_to_wait):
    """
    Set the amount of time to wait for a page load to complete
       before throwing an error.

    :Args:
     - time_to_wait: The amount of time to wait

    :Usage:
        driver.set_page_load_timeout(30)
    """
    try:
        self.execute(Command.SET_TIMEOUTS, {
        'pageLoad': int(float(time_to_wait) * 1000)})
    except WebDriverException:
        self.execute(Command.SET_TIMEOUTS, {
        'ms': float(time_to_wait) * 1000,
        'type': 'page load'})

Here you can find a detailed discussion on set_page_load_timeout

set_script_timeout(time_to_wait)

set_script_timeout(time_to_wait) sets the amount of time that the script should wait during an execute_async_script (Javascript / AJAX Call) call before throwing an error and is defined as :

    def set_script_timeout(self, time_to_wait):
    """
    Set the amount of time that the script should wait during an
       execute_async_script call before throwing an error.

    :Args:
     - time_to_wait: The amount of time to wait (in seconds)

    :Usage:
        driver.set_script_timeout(30)
    """
    if self.w3c:
        self.execute(Command.SET_TIMEOUTS, {
        'script': int(float(time_to_wait) * 1000)})
    else:
        self.execute(Command.SET_SCRIPT_TIMEOUT, {
        'ms': float(time_to_wait) * 1000})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...