In the python webdriver, select the text value from the drop-down box,

Source: Internet
Author: User

In the python webdriver, select the text value from the drop-down box,

In automation, python processes fewer online instances in the drop-down box. The drop-down list in the tutorials written by other predecessors is only relative to the tutorial. For example:

m=driver.find_element_by_id("ShippingMethod")m.find_element_by_xpath("//option[@value='10.69']").click()

Click Next to attribute positioning in the drop-down box!

However, in practical applications, it is impossible to judge by the value. Generally, it is determined by the currently displayed value. Therefore, the tutorial can only teach you how to get started, but the application has to rely on itself.

Start the tutorial.

I. Typical select option format drop-down box

For example, the following html code cannot be determined by the value in the actual automated test, because the value has to be viewed each time. More often, the value is determined by the title value.

<Html> <body> <div class = "menu_bar" style = "margin: 0px 5px 0px 135px;"> <div style = "display: inline; "> <form style =" display: inline "name =" productForm "action =" lib/general/navBar. php "method =" get "> <input type = 'den den 'name = 'csrfname' id = 'csrfname' value = 'csrfguard _ 000000'/> <input type = 'den den 'name = 'csrftoken' id = 'csrftoken' value = '00000'/> test product <select style = "font-size: 80%; position: relative; top:-1px; "name =" testproject "onchange =" this. form. submit (); "> <option value =" 2 "title =" test1: A monitoring system "> test1: A certain monitoring system </option> <option value = "12810" title = "test2: XX capability platform"> test2: XX capability platform </option> <option value = "1332" title = "test3: XX financial system"> test3: financial System </option> <option value = "12684" title = "test4: public platform"> test4: public platform </option> <option value = "1" title = "test5: Beijing haze monitoring" selected = "selected"> test5: beijing haze monitoring </option> <option value = "31823" title = "test6: QQ connection"> test6: QQ connection </option> <option value = "32154" title = "test7: Southern Metropolis Daily"> test7: southern Metropolis Daily </option> <option value = "1528" title = "test8: Unicom platform"> test8: china Unicom platform/option> <option value = "944" title = "test9: mobile platform"> test9: mobile platform </option> </select> </form> </div> </body> 
Test ProductTest1: a certain monitoring systemTest2: XX capability PlatformTest3: XX Financial SystemTest4: public platformTest5: Beijing haze monitoringTest6: QQ connectionTest7: Southern Metropolis DailyTest8: China Unicom platform/option>Test9: mobile platform


For example, how do we choose test6: QQ connection,

Method 1:

# Traverse the drop-down box and select the desired project select = browser. find_element_by_name ("testproject") allOptions = select. find_elements_by_tag_name ("option") for option in allOptions: print "Value is:" + option. get_attribute ("value") print "Text is:" + option. text if 'test6' in option. text: option. click () break

You can also use method 2 on the Internet:

Select (driver. find_element_by_name ("testproject"). select_by_visible_text ("test6: QQ connection ")

However, method 2 requires that all texts be written before selection. If an error is entered, it cannot be selected.

 

Ii. dropdow drop-down box with bootstrap Special Effects

As shown in the drop-down list

Type: Use the following method:

Browser. find_element_by_xpath ("// div [contains (@ class, 'chosen-C')]"). click () time. sleep (1) lis = browser. find_elements_by_xpath ("// ul [@ class = 'chosen-results']/li") for li in lis: if "industry test" in li. text: I. click () breaktime. sleep (2)

You can also retrieve the Element Object list to traverse the object and stop the object after obtaining the desired data.

Find_elements_by_xpath is elements


Conclusion: In the page_object mode in actual automated testing, this is very simple, for example, extracting data.


Reference: http://www.bubuko.com/infodetail-1115484.html

Related Article

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.