Easy Automation---selenium-webdriver (python) (v)

Source: Internet
Author: User

http://www.testclass.net/ Test Tutorial Network, professional Selenium learning website.

This sectionto solves the problem:

    • Level positioning

Scene:

If two controls, their long appearance, also called "Zhang San", the only difference is one in Beijing, one in Shanghai, then we can pass, their cities, districts, streets, to find them.

This problem is often encountered in real-world testing: There are many elements on the page that are essentially the same, and now you need to target one of them. Because the property is basically equivalent, so in the location of the time will be a bit of trouble, it is necessary to use the level of positioning. The parent element is positioned before the descendant element is positioned through the parent element.

<Html><Head><MetaHttp-equiv= "Content-type"Content= "Text/html;charset=utf-8"/><Title>level Locate</Title><ScriptType= "Text/javascript"Async=""Src= "Http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></Script><LinkHref= "Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/css/bootstrap-combined.min.css"Rel= "stylesheet"/></Head><Body><H3>level Locate</H3><DivClass= "Span3"><DivClass= "Well"><DivClass= "Dropdown"><AClass= "Dropdown-toggle"Data-toggle= "Dropdown"Href="#">link1</A><UlClass= "Dropdown-menu"Role= "Menu"Aria-labelledby= "Dlabel"Id= "DropDown1"><Li><ATabIndex= "-1"Href="#">action</A></Li><Li><ATabIndex= "-1"Href="#">another Action</A></Li><Li><ATabIndex= "-1"Href="#">something Else here</A></Li><LiClass= "Divider"></Li><Li><ATabIndex= "-1"Href="#">separated Link</A></Li></Ul></Div></Div></Div><DivClass= "Span3"><DivClass= "Well"><DivClass= "Dropdown"><AClass= "Dropdown-toggle"Data-toggle= "Dropdown"Href="#">link2</a> <ul class=" Dropdown-menu " Role=" menu " Aria-labelledby= "Dlabel" > <li><a tabindex= "-1" href= "#" >Action</a></li> <li>< A tabindex= "-1" href= "#" >another action</a></li> <li><a tabindex= "-1" href= "#" >Something else here</a></li> <li class= "divider" ></li> <li><a tabindex= "-1" href= "#" > Separated link</a></li> </ul> </div> </div> </div> </body> <script src= " Http://netdna.bootstrapcdn.com/twitter-bootstrap/2.3.2/js/bootstrap.min.js "></script>

Save this code to Notepad and save it as a level_locate.html file (note that this page needs to be placed in the same directory as our automation script) and the browser opens:

Here homemade a page, there are two text links, click on two links will pop up the same two drop-down menu, the properties of the two menus are basically the same. So how do I differentiate between finding the corresponding menu item?

Here's how:

#-*-Coding:utf-8-*-From seleniumImportWebdriverFrom Selenium.webdriver.support.uiImportWebdriverwaitImportTimeImportOSDR =Webdriver. Firefox () File_path =‘file:///' + Os.path.abspath (‘Level_locate.html‘) Dr.get (File_path)#Click the Link1 link (pop-up drop-down list) Dr.find_element_by_link_text (‘Link1‘). Click ()#lambda the_driver:the_driver.find_element_by_id ( ' dropdown1< Span style= "COLOR: #800000" > ' ). Is_displayed ()) # "dropdown1 "action " # mouse navigates to child elements webdriver. Actionchains (DR). Move_to_element (menu). Perform () Time.sleep (2  

Positioning ideas:

The specific idea is: first click Show 1 Drop -down menu, and then locate the drop-down menu where the ul, and then locate the UL under a specific link. Here, we locate the action option in the 1 drop-down menu.

---------------------------------------------------------------------

Although I called each line of code before the comments, but it may not be easy to understand, because there are many new things that have not been seen before.

Webdriverwait (DR, 10)

Scans 1 page changes every 500 milliseconds in 10 seconds, ending when the specified element appears. Dr does not explain that the handle of the front operation Webdriver.firefox ()

Is_displayed ()

Whether the element is visible to the user

Class Actionchains (Driver)

Driver: Performing a user action instance webdriver

Generates the user's behavior. All actions are stored in the actionchains object. behavior that is stored through the perform ().

Move_to_element (menu)

Move the mouse to an element, menu above has defined which element he points to

To_element: Component moved to

Perform ()

Perform all stored behavior

------------------------------need our daily work to savor and slowly digest the use of these functions

In fact, verbose so much, we just want to achieve an effect, "The action option is selected in the drop -down list", the mouse move to the option to achieve this effect, but through the program simulation is really troublesome:

--------------------------

Learn more about selenium content:

Functional Test Automation Rollup

Easy Automation---selenium-webdriver (python) (v)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.