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

Source: Internet
Author: User
Tags mail sleep

The focus of this section

Working with Drop-down boxes

Switch_to_alert ()

Accept ()

Dropdown box is our most common kind of page elements, for the general element, we only need to position once, but the contents of the dropdown box needs to be positioned two times, first navigate to the Drop-down box, and then navigate to the dropdown box in the options.

Drop_down.html

 

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.

See more highlights of this column: http://www.bianceng.cnhttp://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 (one page should show 100 results)
    
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 way to locate the "OK" button on the window, but instead use:

Driver.switch_to_alert (). Accept ()

The operation is complete because the window comparison is a unique warning message, so it can be handled in this simple way.

–switch_to_alert ()

A warning (hint) to focus on the page

–accept ()
Accept Warning Tips

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.