Selenium is one of the most popular Web-side automation testing frameworks to automate user actions on products under test. Selenium is open source, and the core component of the Selenium framework is Selenium WebDriver. Selenium WebDriver allows users to execute test cases on different browsers such as Chrome, Firefox, Internet Explorer, Microsoft Edge, and so on. The main advantage of using Selenium WebDriver is its support. NET, Java, C#, Python, etc. You can refer to the official documentation on the Selenium WebDriver architecture for more information.

Although Selenium simplifies the testing of Web sites or Web applications, test developers face many Selenium automation challenges when working with the framework. Let’s take a look at some of the most common challenges that Selenium Automation faces and their good solutions.

False positive success and false positive failure

False success is also a case where the test results are successful, even if they are not. And vice versa, a false positive failure is a test failure that reports an error during script execution even if everything goes as expected. False positives have always been the biggest challenge to automated testing, and Selenium is no exception.

When test engineers run hundreds or thousands of test cases through Selenium scripts, they may encounter unstable tests that show false positives. If left untreated for a long time, the entire automated test project may lose value and testers’ automated test scripts become “junk.”

The stability of test scripts is undoubtedly one of the most common challenges in Selenium automation. At present, there is still a lack of common solutions, but from the perspective of past work experience, it is a good idea to solve this problem from the process of moving the test left, independent test environment, statistical script false positive rate and so on.

Wait for the web page to load JavaScript

Many Web sites now contain Web elements that require JS to load asynchronously, such as drop-down lists based on user selection. Selenium scripts may suddenly fail at runtime with these Web elements. This happens because WebDriver doesn’t handle the time it takes for a web page to fully load. To handle asynchronous loading in Selenium automation for page loading, you need to make WebDriver wait until the full JavaScript for the page is loaded. Before performing tests on any web page, you should ensure that the page, especially one with a lot of JavaScript code, has been loaded. You can use the readyState property, which describes the loading status of the document/web page. A document.readyState state of complete indicates that parsing of the page/document is complete.

"In this example, We will pytest framework used with Selenium "' import pytest from Selenium import webdriver from Selenium. Webdriver. Chrome. The options import Options from selenium.webdriver.common.keys import Keys from time import sleep from contextlib import contextmanager from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.support.expected_conditions import staleness_of @pytest.fixture(params=["chrome"],scope="class") def driver_init(request): if request.param == "chrome": web_driver = webdriver.Chrome() request.cls.driver = web_driver yield web_driver.close() @pytest.mark.usefixtures("driver_init") class BasicTest: pass class Test_URL(BasicTest): def test_open_url(self): self.driver.get("https://www.*****.com/") print(self.driver.title) sleep(5) def wait_for_page_load(self, timeout=30): old_page = self.driver.find_element_by_id('owl-example') yield WebDriverWait(self.driver, timeout).until( staleness_of(old_page) ) def test_click_operation(self): # wait 10 seconds for timeout, then CLICK with self.wait_for_page_load(timeout=10): self.driver.find_element_by_link_text('FREE SIGN UP').click() print(self.driver.execute_script("return document.readyState"))Copy the code

Methods that cannot be extended

Selenium is an outstanding automated testing tool that is open source and makes life easier for Web testers around the world. However, one of the major challenges of Selenium automation is its inability to scale.

The ultimate goal of performing automated tests is to cover more test coverage in less time. There may be a short test build initially, but the product is bound to change with each iteration. This means that you may need to cover more test cases. With Selenium WebDriver, you can only execute tests sequentially and not as effectively as you would like with an automated process, and test scripts are getting slower and slower to execute.

Selenium Grid can now run test cases in parallel, but this has a downside. You cannot fully test a web site or web application across multiple combinations of browsers and operating systems. Because Selenium Grid only facilitates cross-browser testing on a specific browser installed on a local computer. You can leverage the parallel testing capabilities in Selenium to replace linear testing, reducing overall project costs and speeding up product/function iterative delivery when automated tests are performed in parallel.

Working with dynamic content

More and more websites are using dynamic content. Testing websites with static content is relatively easy for Selenium Automation. In this day and age, the content of most websites can vary from visitor to visitor. This means that content is dynamic in nature (ajax-based applications).

For example, e-commerce sites can load different products based on where the user logs in, and content may vary depending on what the user selects from a specific drop-down menu. Because new content takes time to load, it is important to trigger tests only after loading is complete. Because elements on a web page load at different intervals, an error can occur if an element does not already exist in the DOM. This is why working with dynamic content has been one of the most common challenges in Selenium automation.

A simple solution to this problem is to put the thread to sleep for a few seconds, which might provide enough time to load the content. However, this is not a good habit because threads will sleep for that long regardless of whether the required events occur.

From Selenium import webdriver import time from time import sleep driver = webdriver.Firefox() Driver.get ("https://www.*****.com") # Sleep for 10 seconds with or without element time.sleep(10) # Resource release driver.close()Copy the code

A better way to handle this challenge with Selenium WebDriver dynamic content is to use implicit or explicit wait, depending on your requirements.

Wait explicitly to process dynamic content

Using explicit waiting, you can cause Selenium WebDriver to stop execution and wait until certain conditions are met. If you want to set a condition to wait for an exact time period, you can use it with the thread.sleep() function. There are several ways to implement explicit wait, and WebDriver with ExpectedCondition is the most popular option.

from selenium import webdriver from time import sleep from selenium.common.exceptions import NoSuchElementException from  selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.common.by import By from selenium.common.exceptions import TimeoutException from selenium.webdriver.support.ui import WebDriverWait driver = webdriver.Firefox() driver.get("https://www.*****.com") try: myElem_1 = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CLASS_NAME, 'home-btn'))) print("Element 1 found") myElem_2 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'login')) print("Element 2 found") myelem_2.click () sleep(10) # print("No element found") sleep(10) driver.close()Copy the code

In the example above, two searches were made for the URL content under test. The first search is to locate the element to the element home-btn by CLASS_NAME and the maximum duration is 10 seconds.

The second search is clickable element login, with a maximum duration of 10 seconds. If a clickable element exists, perform the click() operation. In both cases, WebDriverWait is used with the ExpectedCondition. The default limit for WebDriverWait to trigger ExpectedCondition is 500 milliseconds until a successful response is received.

Implicitly wait to process dynamic content

Implicitly wait to notify WebDriver to poll the DOM for the presence of Web elements on the page for a specific period of time. The default timeout is 0 seconds. Implicit wait needs to be set up once, and if configured correctly, it will be available for the lifetime of the Selenium WebDriver object.

from selenium import webdriver import time from time import sleep from selenium.common.exceptions import NoSuchElementException from selenium.webdriver.support import expected_conditions as EC from builtins import str driver = webdriver.Firefox() driver.get("https://www.*****.com") driver.implicitly_wait(60) curr_time = time.time() try: Curr_element = driver.find_element_by_class_name(" home-bTN ") except: print(" Element does not exist! ) print (" time "+ STR (int (time. Time () - curr_time)) + '- secs) driver. The close ()Copy the code

Dealing with pop-ups

In some cases, you need to automatically interact with pop-up Windows, which is one of the most common challenges in Selenium automation. There are different types of pop-ups:

  • Simple alert: something that displays some messages.
  • Confirmation alert: Asks the user to confirm the action.
  • Prompt alert: Notifies the user of input.

Although Selenium WebDriver cannot handle Windows-based alerts, it does have the capability to handle Web-based alerts. The switch_to method is used to handle pop-up Windows. To demonstrate, we created a simple HTML file containing the Alert pop-up window implementation.

<! DOCTYPE html> <html> <body> <h2> Demo for Alert</h3> <button onclick="create_alert_dialogue()" name ="submit">Alert Creation</button> <script> function create_alert_dialogue() {alert(" ); } </script> </body> </html>Copy the code

Here is how we use the switch_to method.switch_to.alert.alert to switch to the alert dialog. Once in the Alert Box, you can use the alert.accept() method to receive alerts.

from selenium import webdriver import time from time import sleep from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import TimeoutException from selenium.webdriver.support import expected_conditions as EC from builtins import str driver = webdriver.Firefox() driver.get("file://<HTML File location>") driver.find_element_by_name("submit").click() sleep(5) Alert = driver.switch_to.alert text_in_alert = alert.text sleep(10) alert.accept() print(" OK, skip the warning box ") driver.close()Copy the code

If the page displays an input alert, you can use the send_keys() method to send text to the input box.

from selenium import webdriver import time from time import sleep from selenium.webdriver.support.ui import WebDriverWait from selenium.common.exceptions import NoSuchElementException from selenium.common.exceptions import TimeoutException from selenium.webdriver.support import expected_conditions as EC from builtins import str driver = webdriver.Firefox() driver.get("file://<HTML File location>") driver.find_element_by_name("***").send_keys("***") sleep(5) driver.find_element_by_name("submit").click() sleep(5) try: WebDriverWait(driver, 10).until(EC.alert_is_present(), Alert = driver.switch_to.alert sleep(10) alert. Accept () sleep(10) except TimeoutException: Print (" no warning ") driver.close()Copy the code

Switch browser window

Multi-window testing is undoubtedly one of the common challenges in Selenium automation. Ideally, clicking a button opens a pop-up window that becomes a child window. Once the activity on the child window is complete, the control should be handed over to the parent or upper window. This can be done by using the switch_to.window() method, where window_handle is passed as the input parameter.

When the user clicks the link, a new pop-up window opens, which is called a child window. The switch_to.window method is used to switch back to the parent window. You can switch to the parent window using driver.switch_to.window(window-handle-id) or driver.switch_to.default_content ().

import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys from selenium.webdriver.common.by import By from time import sleep class test_switch_windows(unittest.TestCase): def setUp(self): self.driver = webdriver.Firefox() def test_open_pop_up_window(self): driver = self.driver driver.get("http://www.****.com/html/codes/html_popup_window_code.cfm") title1 = driver.title Parent_window = driver. Window_handles [0] print(parent_window) # iframe driver.switch_to.frame(driver.find_element_by_name('result1')) driver.find_element_by_link_text('show_iframe').click() Window_handles [1] # Parent window will be in the background # child window will be in the foreground driver.switch_to.window(child_window) title2 = Driver. title print(title2) print(child_window) # child_window) sleep(5) def tearDown(self): self.driver.close() if __name__ == "__main__": unittest.main()Copy the code

Unable to test mobile device

Although the Selenium framework has been widely used to test Web sites or Web applications across different combinations of browsers and operating systems, this testing is still limited to non-mobile devices. Therefore, testing Web sites or Web applications for mobile devices is one of the major challenges Selenium Automation faces.

If you perform test automation testing on mobile applications, the most well-known open source framework is Appium.

You can’t automate everything

100% automation is a bragging proposition. It is well known that not all test scenarios can be automated, as some tests require manual intervention. You need to determine how much effort the team should spend on automated testing versus manual testing. While the Selenium framework has features for capturing screen shots, recording videos (overall test execution), and visualizing other aspects of testing, using these features in conjunction with an extensible cloud-based cross-browser testing platform can be of great value.

Generate test report

The primary purpose of any testing activity is to find bugs and improve the overall product. Reports can play a major role in tracking the tests being executed, the output generated, and the test results. Although there are modules that can be used with PyTest and Selenium, such as Pytest_HTML (for Python), the information in the test report may not be exhaustive. Selenium can use similar modules/packages from different types of programming languages (such as Java, C#,.net, etc.), but these languages still have the same problems. Gathering test reports is one of the key challenges in Selenium automation.

Many third-party cloud measurement platforms based on Selenium, as well as many companies’ own reporting frameworks developed by Selenium, generally enrich reporting information in the following aspects:

  • Retrieves build information, such as build test status, individual test status, test run times, errors, and test logs
  • Obtain screen shots by running commands
  • Details about the browser environment

Some can’t

The above are some common challenges in Selenium automation, and there are some limitations in the case of Selenium. The Selenium framework can only be used to test Web applications, that is, not native Windows-based applications. These scenarios may be needed in some cases for security purposes, so it is difficult or never possible for automated tests to bypass some of the strict authentication.


  • The public number FunTester first, more original article: FunTester440+ original article, welcome to pay attention to, exchange, prohibit the third party to reprint without authorization.

Hot article selected

  • Function This interface is used to test albums
  • Open source testing service
  • Performance Testing Topics
  • Graphic HTTP brain map
  • Programming thinking for everyone
  • Mobile phone verification code Login performance test
  • 2020 Tester self-improvement
  • E-book site crawler practices
  • How to perform functional API tests correctly
  • Test case design – the foundation of all testing
  • Fiddler Everywhere is the future