Selenium-webdriver (Python) (10) How to handle a drop-down box

Source: Internet
Author: User

This section focuses on

Handling drop-down boxes

Switch_to_alert ()

Accept ()

The dropdown box is one of our most common page elements, for the general element, we only need to position one at a time, but the contents of the drop-down box needs to be positioned two times, first navigate to the dropdown box, and then navigate to the dropdown box inside the options.

Drop_down.html

Save the above code in HTML browser open you will see one of the simplest and most common drop-down boxes, the drop-down list has several options.

Now let's choose the $10.69 in the dropdown list.

#-*-coding=utf-8 from    Selenium import webdriver    import os,time    driver= webdriver. Firefox ()    file_path =  ' file:///' + os.path.abspath (' drop_down.html ')    driver.get (File_path)    Time.sleep (2)    m=driver.find_element_by_id ("ShippingMethod")    M.find_element_by_xpath ("//option[@value = ' 10.69 ']). Click ()    time.sleep (3)    driver.quit ()

Analytical:

This may be different from the previous operation, first to navigate to the elements of the drop-down box, and then select the options in the drop-down list to click on the action.

View more Highlights: http://www.bianceng.cn/Programming/extra/

m=driver.find_element_by_id ("ShippingMethod")

M.find_element_by_xpath ("//option[@value = ' 10.69 ']"). Click ()

Baidu Search Settings Dropdown box operation

#-*-coding=utf-8 from    Selenium import webdriver    import os,time    driver= webdriver. Firefox ()    driver.get ("http://www.baidu.com")        #进入搜索设置页    driver.find_element_by_link_text ("Search Settings"). Click ()        #设置每页搜索结果为100条    m=driver.find_element_by_name ("NR")    M.find_element_by_xpath ("//option[@ Value= '). Click ()    time.sleep (2)        #保存设置的信息    driver.find_element_by_xpath ("//input[@value = ' Save Settings ']. Click ()    time.sleep (2)    Driver.switch_to_alert (). Accept ()            #跳转到百度首页后, search table (100 results on a page)    driver.find_element_by_id ("kw"). Send_keys ("Selenium")    driver.find_element_by_id ("su"). Click ()    Time.sleep (3)    driver.quit ()

Analytical:

When we save Baidu's settings will pop up a OK button; We did not follow the usual method to locate the "OK" button on the pop-up window, but instead use:

Driver.switch_to_alert (). Accept ()

This is done because the window comparison is a unique warning message, so it can be handled in such a simple way.

–switch_to_alert ()

A warning that focuses on the page (hint)

–accept ()
Accept warning Prompts

Selenium-webdriver (Python) (10) How to handle a drop-down box

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.