Reputation: 21
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:
WebDriverWait(self.driver,self.timeout).until(EC.presence_of_element_located((By.XPATH, element)))
which returns a WebElement. Two things about this line:
I think its not respecting the self.timeout
time due to capa["pageLoadStrategy"] = "none"
but I'm not sure
Its very unstable, sometimes it runs fast, sometimes very slow.
driver.execute_script("document.getElementsByClassName('alert alert-danger ng-binding ng-scope')[0].innerText")
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
Reputation: 193308
Addressing your concern:
capa["pageLoadStrategy"] = "none"
to configure Selenium not to wait for the dom to completely load just won't be limited to tradeoff in performance only, but also would impact the stability of your tests as moving forward you need to identify WebElement depending on their presence, visibility and clickabilityparticular 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