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 tocapa["pageLoadStrategy"] = "none"
but I'm not sureIts 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.