When writing automation, it often happens that elements can’t be located. The following situations and solutions are summarized:

· Form nesting

· A new window opens

· Positioning method is not unique, or there is no such element

· Xpath positioning is incorrect

1. Form nesting

· Find the cause by looking up the location element to see if there is a

· Solution using switch_to_frame(“name value “)

driver = webdriver.Chrome()

Driver. Switch_to_frame (” name value “)

If the iframe does not have a name value, locate the iframe and transfer the locating object to switch_to_frame

Frame =driver.find_element_by_xpath(‘ Where to locate ‘)

driver.switch_to_frame(frame)

Once done, you can jump out of the current IFrame with the switch_to.parent_content() method

2. A new window is opened

· If the element to be searched is in a new window, you need to switch to the new window to locate it.

· Switch to the new window method

Get a handle to the newly opened window

handle = self.driver.current_window_handle

Get all window handles

handles = self.driver.window_handles

# Switch to a new window

for newhandles in handles:

if newhandles ! = handle:

self.driver.switch_to_window(newhandles)

3. The positioning method is not unique, or there is no such element

· Check to see if the element is locatable using the document.querySelector() method of F12’s console tag to verify that the element can be found

· There are multiple elements and you need to locate the specific element. < class=”picture” > < class=”picture”

driver.find_elements_by_class_name(‘picture’)[0]

4. The xpath location mode is incorrect

To check for xpath errors, open Chrome, press F12, CTRL + F, type the xpath you want to check, and see if you can locate the correct element.