Get and set the value of the dropdown menu using Python and Selenium

This article uses Python 3.6.0 and Selenium 3.4.0 (WebDriver is Google Chrome).

I had a hard time getting and setting the drop-down menu, so I will leave a note.

Make Select tag available from WebElement

select.html



<select name=”color”>
<option value=”red”>Red</option>
<option value=”blue”>Blue</option>
<option value=”green”>Green</option>
</select>

Suppose you have a drop-down menu that says.

Select.py



#Call a function to change to an element that can be handled by the Select tag
from selenium.webdriver.support.ui import Select

#Get the element normally
color_element = browser.find_element_by_name('color')

#Change the acquired element to the element corresponding to the Select tag
color_select_element = Select(color_element)

#Specify the value you want to select
color_select_element.select_by_value('blue')

If you write like this, "blue" will be selected.

in conclusion

At first I couldn't set the value with send_keys and I was really into it, I never thought I had to cast (?). Other than that, the tag needs to be done like this. I have no plans to do anything at the moment, but automation is fun.

Recommended Posts

Get and set the value of the dropdown menu using Python and Selenium
Get and estimate the shape of the head using Dlib and OpenCV with python
Get the value selected in Selenium Python VBA pull-down
[Django 2.2] Sort and get the value of the relation destination
The story of Python and the story of NaN
Note: Get the first and last items of Python OrderedDict non-destructively
How to get followers and followers from python using the Mastodon API
[Python] How to get the first and last days of the month
Get the title and delivery date of Yahoo! News in Python
Reboot the router using Python, Selenium, PhantomJS
Get the value of the middle layer of NN
Set the process name of the Python program
[Python] Get the character code of the file
Get a capture of the entire web page in Selenium Python VBA
Get the number of articles accessed and likes with Qiita API + Python
Get the return value of an external shell script (ls) with python3
Get the contents of git diff from python
I tried web scraping using python and selenium
Summary of the differences between PHP and Python
[Python] Get / edit the scale label of the figure
[Python] Get the main topics of Yahoo News
Get the caller of a function in Python
The answer of "1/2" is different between python2 and 3
Specifying the range of ruby and python arrays
Find the divisor of the value entered in python
Compare the speed of Python append and map
[Python] Get the last updated date of the website
Try using the collections module (ChainMap) of python3
Find the geometric mean of n! Using Python
About the * (asterisk) argument of python (and itertools.starmap)
A discussion of the strengths and weaknesses of Python
[Python] Get the day of the week (English & Japanese)
Get the update date of the Python memo file.
Python programming: I tried to get (crawling) news articles using Selenium and BeautifulSoup4.
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
Get the stock price of a Japanese company with Python and make a graph
Get the last element of the array by splitting the string in Python and PHP
I checked the reference speed when using python list, dictionary, and set type in.
Get the title of yahoo news and analyze sentiment
Explanation of the concept of regression analysis using python Part 2
How to get the number of digits in Python
[Python of Hikari-] Chapter 06-02 Function (argument and return value 1)
I want to get custom data attributes of html as elements using Python Selenium
Get the value of a specific key in a list from the dictionary type in the list with Python
[Python] Get the official file path of the shortcut file (.lnk)
[Python] Get the text of the law from the e-GOV Law API
Cut a part of the string using a Python slice
[Python] Calculate the average value of the pixel value RGB of the object
The process of installing Atom and getting Python running
Get information equivalent to the Network tab of Chrome developer tools with Python + Selenium
Python --Explanation and usage summary of the top 24 packages
[python] Get the list of classes defined in the module
the zen of Python
I tried to automate the article update of Livedoor blog with Python and selenium.
Visualize the range of interpolation and extrapolation with python
Get the return code of the Python script from bat
[C language] [Linux] Get the value of environment variable
Referencing and changing the upper bound of Python recursion
The pain of gRPC using Python. November 2019. (Personal memo)
I checked out the versions of Blender and Python
Send and receive Gmail via the Gmail API using Python