scene

In the processing drop-down box (select) When selenium It provides us with a series of convenient methods , We just need to use selenium.webdriver.support.select.Select Class, just a little encapsulation .

Here are some of the methods we often use

  • options: Go back to all the options in the drop-down box
  • all_selected_options: Returns all selected options
  • select_by_value(value): adopt option Of value Value to select
  • select_by_index(index) adopt option Choose in the right order
  • select_by_visible_text(text): adopt option Of text Choose

Code

s7.html

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form enctype="multipart/form-data">
<div> <textarea name="meno" >123456</textarea> <select name="city" >
<option value="beijing"> Beijing </option>
<option value="shanghai"> Shanghai </option>
<option value="nanjing" selected="selected"> nanjing </option>
<option value="chengdu"> Chengdu </option>
</select> <input type="submit" value=" Submit " />
<input type="reset" value=" Reset " />
</div>
</form>
</body>
</html>

select--tag.py

#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: select--tag.pay
@time: 2018-05-10 10:44
'''
from selenium import webdriver
from selenium.webdriver.support.select import Select
from time import sleep
import os if 'HTTP_PROXY'in os.environ:
del os.environ['HTTP_PROXY'] dr = webdriver.Firefox()
file_path ='file://'+ os.path.abspath('s7.html')
print (file_path)
dr.get(file_path) city_selector = Select(dr.find_element_by_tag_name('select')) # Return all options
print(city_selector.options)
sleep(1) # Return all selected options
print(city_selector.all_selected_options)
sleep(1) # adopt option Of value Value to choose Shanghai
city_selector.select_by_value('shanghai')
sleep(2) # adopt index Choose , For example, choose the second 4 term
city_selector.select_by_index(3)
sleep(1) # adopt option Of text Choose
city_selector.select_by_visible_text(' Beijing ') dr.quit()

uniquefu Python+Selenium Study --select More articles about

  1. 【python+selenium Study 】Python Common mistakes :IndentationError: unexpected indent

    New to python+selenium Way of learning , There will always be such and such problems .IndentationError: unexpected indent, I have stepped into this pit several times , Simply record it . We all know Python Antithetical generation ...

  2. Python+Selenium Study -- Automated test model

    Preface An automated testing framework is an integration system , In this system, the function library of test function is included . Test data sources . Test object identification criteria , And a reusable module . The development of automated testing framework has gone through several stages , Module driven testing . Data driven testing . Yes ...

  3. Python+Selenium Study -- Automatic generation HTML Test report

    Preface After the script runs , In addition to the log.txt File to see the running log , We hope to produce a beautiful test report to show the results of the use case execution .        HTMLTestRunner yes Python Standard library unit ...

  4. Python+Selenium Study -- Abnormal screenshot

    Preface Webdriver Provide error screenshot function get_screenshot_as_file(), Can help us track bug, When the script cannot continue , get_screenshot_as_file() Function will intercept ...

  5. Python+Selenium Study --cookie Handle

    scene Sometimes we need to verify that there is a cookie, Because based on the real cookie It's impossible to pass white box and integration tests .webdriver Can read . Add and remove cookie Information .webdrive ...

  6. Python+Selenium Study -- Control browser control bar

    scene occasionally web The elements on the page are not directly visible , Even maximize the browser , We still need to drag the scroll bar to see the elements we want to manipulate , At this time, we need to control the drag of the page scroll bar , But scrollbars are not elements on the page , Can use JavaScrip ...

  7. Python+Selenium Study -- Paging

    scene We're testing a web When applied , Page flipping often happens , Here's the flipping scene Code #!/usr/bin/env python # -*- codinfg:utf-8 -*- ''' @author: J ...

  8. Python+Selenium Study -- Drop down box processing

    scene The drop-down box is also web Very common features on the page ,webdriver For general drop-down box processing is also quite simple , To locate the contents of the drop-down box , First you need to navigate to the drop-down box : This kind of secondary positioning Drop down box generally has the following two ways : Mouse up ...

  9. Python+Selenium Study -- Window switching and operation elements

    scene Sometimes we're testing one web Multiple browser windows will appear in the application , stay selenium1.0 It's more difficult to deal with this problem in our country .webdriver Provides a correlation method, which can easily switch between multiple windows and operate on different windows ...

Random recommendation

  1. Storm Build a stand-alone environment

    1 Software to install To use storm First, install the following tools :python.zookeeper.zeromq.jzmq.storm 1.1 install zeromq wget http://download.zer ...

  2. Magento File system directory structure

    magento │  .htaccess│  cron.php // System cron Program , modify linux Of cron function , Join in magento Some of the timing processing │  cron.sh│  favicon.ico ...

  3. `cocos2dx Incomplete ` UI Parsing module

    Yesterday in cocos2dx In a group of , I met an anonymous x My friend asked me a question , It's about ui Of . He USES c++ Wrote a lot of ui Encapsulate nodes , Used to achieve a variety of different effects in the game . And now I want to switch to lua, So try to reuse yourself with the least cost ...

  4. The most comprehensive PHP Development Android Applications

    The first part refers to Android Write directly on the mobile phone of the system PHP Script code and run immediately : The second part continues to explain how to write a good PHP The script code is packaged into akp The installation files . First , Install two apk package . One is SL4A(Scri ...

  5. Linux Kernel Null pointer reference local denial of Service Vulnerability (CVE-2013-5634)

    Vulnerability version : Linux kernel Vulnerability description : BUGTRAQ ID: 61995 CVE(CAN) ID: CVE-2013-5634 Linux Kernel yes Linux The kernel of the operating system . Apply to ...

  6. CentOS7 install chrome browser

    This article mainly records how to CentOS7.0 Installation on chrome. 1. To configure yum Download source : In the catalog /etc/yum.repos.d/ Next new file google-chrome.repo, And add... To the file ...

  7. Prolog There can't be outside DOCTYPE Statement . Processing resources &#39;http://192.168.115.152:8082/api/EmpApi.aspx&#39; An error occurred when . The first 3 That's ok , Location : 11

    stay Default.aspx Delete the following code from the code ( Other windows are deleted in the same way ): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitiona ...

  8. C++ Single case

    template<class T> class Singleton { public: using object_type = T; struct object_creator { obj ...

  9. cookie Follow session Self introduction.

    Cookie What is it? ? cookie The straight point is a key value pair saved in the user's browser , for instance , You are now logged into Jingdong Mall , After you shut down the browser , You open Jingdong again , You can still operate your account , Goods that have been purchased , All orders are ...

  10. create-react-app Create a project and use git Uploaded to the GitHub And show the preview effect

    1. Create a local folder where the project is located 2.npm -g create-react-app 3. In this folder create-react-app react-demo ( Project name ) 4.cd react-d ...