Several methods with regard to the positioning element selenium automation to solve the problem

Can not perform positioning elements encountered a problem and locate the click operation, without further ado, straight to the point:

1. positioning elements not generally have the following three cases, you can if you encounter a condemnation Ha

a. elemental looking for is not in the current window

Solution: Use driver.switch_to.window () switching window, usually in the plurality of windows, using a window to the last switch will be able to

First of all to get the current window = all_win driver.window_handles

Then switch to the last window to driver.switch_to.window (all_win [-1]) or driver.switch_to.window (driver.window_handles [-1])

Note: Most of the new window is opened last, the direct use [-1] to index, get the current window handle with driver.getWindowHandle ()

b. the current element is not in the current page, but in the frame or Iframe framework

Solution: First check yuan page to see if there is frame or iframe tag can also be directly encoded using frame_names = find_elements_by_tag_name ( 'iframe') or ( 'frame') elemental find, and put frame_names variable list

Then driver.switch_to.frame (frame_names [0]) is switched to the first frame in a frame, depending on the length of frame_names, this cable starting from 0 to len (frame_names) -1 a frame, if a direct switch to the last frame use rame_names [-1] to subscript

At this time, the positioning element then driver.find_element_by_xxx method can locate to.

Note: You can use a multi-layer embedded in an iframe to driver.switch_to.parent_frame () is switched to the one iframe , can also be used to switch to the Home Page driver.switch_to.default_content ()

c. page loads slowly, it has not been loaded on the positioning elements

Solution: Use webdriverwait display or the like sleep waiting setting the wait time before positioning element

 

Guess you like

Origin www.cnblogs.com/oscarforever/p/11573943.html