3

The script I am running does not return consistent data from run to run. I believe as I iterate through pages, it is not waiting for all pages to load Javascript and AJAX in full. In an attempt to remedy this I added the explicit wait below, but it returns the following error:

ConnectionRefusedError: [WinError 10061] No connection could be made because the target machine actively refused it

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

browser = webdriver.Chrome()
url = 'http://www.website.com'
browser.get(url)
try:
    element = WebDriverWait(browser, 10).until(EC.presence_of_all_elements_located((By.ID, "results-main")))
finally:
    browser.quit()

print (browser.page_source)

An implicit wait will run without errors, but it also does not return consistent source code.

1
  • You cannot get page source code because of browser.quit(). Note that block of code after finally operator executes always. So you might need to execute browser.page_source before browser.quit() Commented Apr 5, 2017 at 20:19

1 Answer 1

3

Im assuming you are getting this exception at browser.page_source This is because you are doing browser.quit Quit() - It is used to shut down the web driver instance or destroy the web driver

you will need to get the page source before you quit either by adding the print statement at the end of your try block or moving the quit after you print :)

Sign up to request clarification or add additional context in comments.

5 Comments

This is correct. My understanding was that the Quit() was only being called if the wait time was exceeded but that is not the case.
no, a finally block is a way to ensure the code inside it is always executed, remove and see how it will work
@Farmer Replace finally with catch and you will probably get the behavior you intended. You should probably take some time to read the docs on try-catch-finally to better understand how they work.
@JeffC, in Python it is actually try-except-else-finally construction... OP should replace finally with except
@Andersson ah... I'm not a python guy. I should have looked it up instead of assuming it's the same as Java and C#. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.