Selenium-python element positioning skills (2)

In python-selenium element positioning, there are many tips, here is the summary

 Tip 1: When the related elements have intersections, a fixed wait must be added. During the overall debugging, the fixed wait should be appropriately increased to maintain the stability of the code. Selecting the drop-down box when pulling down sometimes needs to wait.

When encountering two elements that have an interdependence (sequential) relationship, when reloading, it takes time to wait, and a fixed wait must be added (Note: be sure to wait fixedly, implicit wait is invalid)

For example:
the selection of the drop-down box, for example, select province> city, after selecting the province, you must add a fixed wait and then go to select the city.
Code example:
driver.find_element (By.XPATH, "// div [@ id = 'project_chosen']"). Click ()
#Dropdown of the drop-down box one time.sleep (1)
driver.find_element (By.XPATH , "/ [@ title = '
提提 项1']"). click () #The input of the value of the drop-down box time.sleep (2) #Need to wait for a fixed time, because there is a dependency
driver.find_element (By.XPATH, " // div [@ id = 'openedBuild_chosen'] "). click ()
#Fix the wait box and select the drop-down box 2 driver.find_element (By.XPATH," / [@ title = 'OA2.7.2Version1'] " ) .click () #Select the value of the sub-option in the drop-down box 2, be sure to check the value of the sub-option

 

 Tip two, the same interface, when the element attributes are the same, but not the same element, you can use multi-attribute identification, otherwise you may locate the element error, or you can use different positioning methods.

 

 Tip three, some elements cannot be located, it may be that the element is no longer visible at the current location of the interface, the solution

a, you can scroll down

b. Consider maximizing the browser

c. Move the browser down to the position where the element is visible.

Examples:

element = driver.find_element (By.XPATH, "// div [@ id = 'mailto_chosen']") #Currently invisible elements that require a scroll bar to be operated 
driver.execute_script ('arguments [0] .scrollIntoView (); ', element)
element.click ()
time.sleep (1) ## You must add wait, otherwise it is not accurate, and then go to operate the subsequent elements
driver.find_element (By.XPATH, "// div [@ id = 'mailto_chosen'] / div / ul / li [@ title = 'C: Test001']"). click () ## Can be positioned in layers, First navigate to the div, then navigate to the sub-options below the div for input.

 

 Tip four: When you need to debug the function of a module, first tune up the small functions that can be split separately, and then debug the whole

 

 Tip five: When the element cannot be located, first locate the superior of the element, and then locate the element.

For example: 
driver.find_element (By.XPATH, "// div [@ id = 'mailto_chosen'] / div / ul / li [@ title = 'C: Test001']"). Click () ## Can be layered Positioning, first positioning to the div, then positioning to the sub-options below the div for input.
or:
driver.find_element (By.XPATH, "// td [contains (@ title, 'Manual modification commission failure')]"). click () #Click on the title to locate the current line first, which can be seen by element positioning. 
## According to the link, enter the BUG details page
driver.find_element (By.XPATH, "// td [contains (@ title, 'Manual modification commission failure')] / a"). Click () # Then click on the relative of the current line Path way to locate the clickable BUG link and click to enter the BUG details
time.sleep (1)




 

 

 

Guess you like

Origin www.cnblogs.com/123anqier-blog/p/12682843.html