Random “Element is no longer attached to the DOM” StaleElementReferenceException

Yes, if you’re having problems with StaleElementReferenceExceptions it’s because there is a race condition. Consider the following scenario: WebElement element = driver.findElement(By.id(“foo”)); // DOM changes – page is refreshed, or element is removed and re-added element.click(); Now at the point where you’re clicking the element, the element reference is no longer valid. It’s close to … Read more

How to run Selenium WebDriver test cases in Chrome

You need to download the executable driver from: ChromeDriver Download Then use the following before creating the driver object (already shown in the correct order): System.setProperty(“webdriver.chrome.driver”, “/path/to/chromedriver”); WebDriver driver = new ChromeDriver(); This was extracted from the most useful guide from the ChromeDriver Documentation.

Can Selenium interact with an existing browser session?

This is a duplicate answer **Reconnect to a driver in python selenium ** This is applicable on all drivers and for java api. open a driver driver = webdriver.Firefox() #python extract to session_id and _url from driver object. url = driver.command_executor._url #”http://127.0.0.1:60622/hub” session_id = driver.session_id #’4e167f26-dc1d-4f51-a207-f761eaf73c31′ Use these two parameter to connect to your driver. … Read more

How to save and load cookies using Python + Selenium WebDriver

You can save the current cookies as a Python object using pickle. For example: import pickle import selenium.webdriver driver = selenium.webdriver.Firefox() driver.get(“http://www.google.com”) pickle.dump( driver.get_cookies() , open(“cookies.pkl”,”wb”)) And later to add them back: import pickle import selenium.webdriver driver = selenium.webdriver.Firefox() driver.get(“http://www.google.com”) cookies = pickle.load(open(“cookies.pkl”, “rb”)) for cookie in cookies: driver.add_cookie(cookie)

Selenium C# WebDriver: Wait until element is present

Using the solution provided by Mike Kwan may have an impact in overall testing performance, since the implicit wait will be used in all FindElement calls. Many times you’ll want the FindElement to fail right away when an element is not present (you’re testing for a malformed page, missing elements, etc.). With the implicit wait … Read more

Can Selenium WebDriver open browser windows silently in the background?

If you are using Selenium web driver with Python, you can use PyVirtualDisplay, a Python wrapper for Xvfb and Xephyr. PyVirtualDisplay needs Xvfb as a dependency. On Ubuntu, first install Xvfb: sudo apt-get install xvfb Then install PyVirtualDisplay from PyPI: pip install pyvirtualdisplay Sample Selenium script in Python in a headless mode with PyVirtualDisplay: #!/usr/bin/env … Read more