SoFunction
Updated on 2024-10-26

Python selenium_webdriver dropdown box operation guide

Environment Setup

First of all, let's build the base environment for the foundation

1. install python base environment (python's base environment is too simple to mention here)

2. Installation is complete python we need to install the basic toolkit pip python, under normal circumstances, the installation of python will be the installation of pip base package, but there are some people do not have to install.

i. Downloadpip Install the package, remember what exe file to look for here, and install it directly with the source code.

ii. Decompressionpip-9.0.If you want to install pip, run python to install pip;

After the installation is complete, open the command line as administrator and run pip install selenium to install selenium. (Setting up the environment is that simple.)

4. Install the webdriver driver, the driver download needs FQ, since we want to learn automation, FQ must be very easy, I use this side of the chrome browser, so download thechromedriverDrive.

IEdriver Download: /

Note: Due to the different versions of the browser please select the driver version according to their own situation, if the version selection does not match, in the open browser will be prompted to report an error, when you see the picture below, it means that your version is not compatible.

I have Chrome version 58.0.3029.81 and chromedriver version 2.29 installed.

5. Remember to store the webdriver driver in the python installation directory, i.e. in the same level directory, otherwise you won't be able to open the browser.

Python selenium_webdriver dropdown box manipulation

Dropdown box operations need to be introduced into the Select class with: from import Select:

from selenium import webdriver    # Introducing webdriver
from  import Select    # Introducing Select
from time import sleep    # Introduction time
driver = ()    # Open Firefox
('file:///C:/Users/CY-MHQ/Desktop/%E6%B1%87%E6%99%BA%E5%8A%A8%E5%8A%9B/%E8%87%AA%E5%8A%A8%E5%8C%96/4select%20-%20%E4%B8%8B%E6%8B%89%E6%A1%')    # Go to page
select_ele = driver.find_element('id','nr')    # Locate the dropdown box with id nr and name it select_ele
select_ob = Select(select_ele)    # Generate instance objects for dropdown boxes
select_ob.select_by_index(2)    #Select the third one in the drop-down box

The methods in the class are:

  • select_by_index('index') # select by index, index starts at 0
  • select_by_value('value') # select by value's value
  • select_by_visible_text('text') # select by text
  • options # Returns all options
  • is_multiple # Determines if the value is multiple, returns True if it is, otherwise returns None.
  • first_selected_option # returns the first selected option
  • deselect_by_visible_text('text') # deselect by text
  • deselect_by_value('value') # deselect by value
  • deselect_by_index('index') # deselect by index
  • all_selected_options # return all selected options
  • deselect_all() # deselect all

In the above method, 5,6,10,11 can only be used for multiple choices.

summarize

to this article on Python selenium_webdriver drop-down box operation guide to this article, more related Python selenium_webdriver drop-down box operation content please search for my previous posts or continue to browse the following related articles I hope you will support me in the future!