PayadorPerseguido
PayadorPerseguido

Reputation: 21

What is the most efficient way to check if element exists in Selenium

Context: I mean to multi-thread several browsers instances and execute processes in them.

Reason for question: I would like to know what is the most efficient/less consuming way to check for element in python selenium. I have tried two methods which i'll show below, and a little understanding of mine about each of them.

First of all, this is my the function which returns the driver instance:

def open_driver():
    chrome_options = webdriver.ChromeOptions()
    prefs = {"profile.default_conte nt_setting_values.notifications" : 2}
    chrome_options.add_experimental_option("prefs", prefs)
    chrome_options.add_argument("start-maximized")
    chrome_options.add_argument('ignore-certificate-errors')
    chrome_options.add_experimental_option('excludeSwitches', ['enable-logging'])

    capa = DesiredCapabilities.CHROME
    capa["pageLoadStrategy"] = "none"

    driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome, desired_capabilities=capa)

    return driver

Note this particular line:

capa = DesiredCapabilities.CHROME
capa["pageLoadStrategy"] = "none"

From my understanding, this will tell selenium not to wait for the dom to completely load. This is a tradeoff in performance which I had to choose, because this particular page would sometimes get stuck endlessly in document.readyState == interactive

So I have basically two options that I know of in checking if element exists (I'd appreciate suggestions too), which are:

This inside a try: except: approach seems to be very much faster in execution compared to the one above, but it seems to overload the browser and then the execution displays errors (error when fetching data from the server) more often

With this being said, and reiterating that I'm new to this, I thank you for taking your time in reading my question.

PS: I'm all in for suggestions, improvements and specially corrections.

Upvotes: 1

Views: 652

Answers (1)

undetected Selenium
undetected Selenium

Reputation: 193308

Addressing your concern:


Conclusion

particular page would sometimes get stuck endlessly would be relatively easier to address. However, bypassing

document.readyState == interactive

and favouring:

capa["pageLoadStrategy"] = "none"

and configuring Selenium WebDriver not to wait for the dom to completely load is not only a tradeoff in performance but also a barrier where you are forced to use presence_of_element_located() instead of visibility_of_element_located() and induce chaos and instability.

Upvotes: 1

Related Questions