When writing automation, the situation is not positioning elements often occur. Summed up the following situations and solutions:
· Nested Form
· Open a new window
· Targeting is not unique, or the absence of the element
· Xpath targeting error
1. Nested Form
· Find the cause
along the positioning element up looking to see if there is <iframe> tag, find instructions for locating this element, you must first switch to this form, repositioning elements.
· Solutions
need to use switch_to_frame ( "name value")
driver = webdriver.Chrome()
driver.switch_to_frame("name值")
If the name does not iframe value, iframe to first locate, and then locate the object passed
switch_to_frame
frame = driver.find_element_by_xpath ( 'positioned where')
driver.switch_to_frame(frame)
After completion, () method out of the current through the iframe switch_to.parent_content
2. Open a new window
· Elements to look for in the new window opens, the need to switch to the new window to locate.
· Switch to the new window method
# Obtain a newly opened window handle
handle = self.driver.current_window_handle
# Get all window handle
handles = self.driver.window_handles
# Switch to the new window
for newhandles in handles:
if newhandles != handle:
self.driver.switch_to_window(newhandles)
3. targeting methods are not unique, or the element is not
• Check whether the element can be positioned, through the console tag F12 through document.querySelector () method of locating, to verify if you can find elements
· There are multiple elements, the need to locate specific elements.
As follows: a plurality of class = "picture" of the elements, the need to locate a first class = "picture"
driver.find_elements_by_class_name('picture')[0]
4.xpath targeting error
Check whether the wrong xpath, open the Chrome, press F12, ctrl + f, the input to check xpath, to see if the correct positioning element.