selenium element positioning solutions to problems

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.

4fc7122b71f14c55b538a7e40b2dd0c5.png

· 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

38f3617ce21d482d879c5ece3a1c7ce4.png

· 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"

4fdc444ad1844c409a94199494350ef9.png

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.

988d811dbe244beda1a86f9ef62cd1a3.png

Guess you like

Origin blog.csdn.net/IT_studied/article/details/90412821