Checking if an element exists with Python Selenium

For a): from selenium.common.exceptions import NoSuchElementException def check_exists_by_xpath(xpath): try: webdriver.find_element_by_xpath(xpath) except NoSuchElementException: return False return True For b): Moreover, you can take the XPath expression as a standard throughout all your scripts and create functions as above mentions for universal use. I recommend to use CSS selectors. I recommend not to mix/use “by id”, “by … Read more

What is the difference between a CSS and XPath selector? And which is better with respect to performance for cross-browser testing?

CSS selectors perform far better than XPath selectors, and it is well documented in Selenium community. Here are some reasons: XPath engines are different in each browser, hence making them inconsistent Internet Explorer does not have a native XPath engine, and therefore Selenium injects its own XPath engine for compatibility of its API. Hence we … Read more

How can I get text of an element in Selenium WebDriver, without including child element text?

Here’s a general solution: def get_text_excluding_children(driver, element): return driver.execute_script(“”” return jQuery(arguments[0]).contents().filter(function() { return this.nodeType == Node.TEXT_NODE; }).text(); “””, element) The element passed to the function can be something obtained from the find_element…() methods (i.e., it can be a WebElement object). Or if you don’t have jQuery or don’t want to use it, you can replace … Read more

“selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element” while clicking a ‘Next’ button with Selenium

This error message… selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {“method”:”name”,”selector”:”submitNext”} (Session info: chrome=66.0.3359.170) (Driver info: chromedriver=2.36.540470) …implies that the ChromeDriver was unable to locate the desired element. Locating the desired element As per the HTML you have shared to click on the element you can use either of the following Locator Strategies: … Read more

Python – Requests, Selenium – passing cookies while logging in

I finally found out what the problem was. Before making the post request with the requests library, I should have passed the cookies of the browser first. The code is as follows: import requests from selenium import webdriver driver = webdriver.Firefox() url = “some_url” #a redirect to a login page occurs driver.get(url) #storing the cookies … Read more