[Python] Automatically operate the browser with Selenium

This section describes the basic usage when using Selenium with Python.

Library installation

pip install selenium

When importing with python

from selenium import webdriver
from selenium.webdriver.support.select import Select

Transition to the specified URL

#Launch Chrome
driver = webdriver.Chrome()

#Transition to the specified URL
driver.get("Specified URL")

Type in the text box

After getting the name attribute etc., enter characters in the text box.

#Enter characters in the text box of the specified name attribute
element = driver.find_element_by_name("name attribute")
element.send_keys("text")

#Enter characters in the text box with the specified id attribute
element = driver.find_element_by_id("id attribute")
element.send_keys("text")

#Enter characters in the text box of the specified class attribute
element = driver.find_element_by_class_name("class attribute")
element.send_keys("text")

#Enter characters in the text box of class attribute B in class attribute A
element = driver.find_element_by_class_name("class attribute A").find_element_by_name("class attribute B")
element.send_keys("text")

Close the confirmation dialog


#Press the confirmation dialog
alert = driver.switch_to.alert
alert.accept()

Press the button

When you press the button, you can either execute the JavaScript in onclick or press the specified attribute.

#JavaScript execution
driver.execute_script("JavaScript name")

#Press the specified class attribute
driver.find_element_by_class_name("class attribute").click()

Close web browser

#Close an open web browser
driver.quit()

Example: How to enter your login ID

When entering the login ID and password from the Qiita login screen, do as follows.


from selenium import webdriver
from selenium.webdriver.support.select import Select

#Launch Chrome
driver = webdriver.Chrome()

#Transition to the specified URL
driver.get("https://qiita.com/login")

#Enter "Username or Email Address"
driver.find_element_by_name("identity").send_keys("Login ID")

#Enter "Password"
driver.find_element_by_name("password").send_keys("password")

#Press "Login to Qiita"
driver.find_element_by_name("commit").click()

#Close web browser
driver.quit()

reference

https://tanuhack.com/selenium/#CSSXPath https://www.seleniumqref.com/index.html

Recommended Posts

[Python] Automatically operate the browser with Selenium
Operate your browser using the Selenium Web Driver Python bindings
Display Python 3 in the browser with MAMP
Operate Firefox with Selenium from python and save the screen capture
Operate Kinesis with Python
ScreenShot with Selenium (Python)
Operate Blender with Python
Scraping with Selenium [Python]
Operate Excel with Python (1)
Operate Excel with Python (2)
Operate Paints Chainer with Selenium to automatically color images in the directory
[Python] How to set the (client) window size inside the browser with Selenium
Operate Excel with Python openpyxl
Operate TwitterBot with Lambda, Python
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Python: Working with Firefox with selenium
Scraping with Selenium in Python
[Note] Operate MongoDB with Python
[Python] [SQLite3] Operate SQLite with Python (Basic)
Call the API with python3.
Automatically translate DeepL into English with Python and Selenium
I want to automatically attend online classes with Python + Selenium!
Extract the xz file with python
Operate a receipt printer with python
Python in the browser: Brython's recommendation
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
Try to operate Facebook with Python
Get the weather with Python requests
Get the weather with Python requests 2
Find the Levenshtein Distance with python
Hit the Etherpad-lite API with Python
Install the Python plugin with Netbeans 8.0.2
I liked the tweet with python. ..
Automatically build Python documentation with Sphinx
Master the type with Python [Python 3.9 compatible]
Discord Bot with recording function starting with Python: (5) Directly operate the Discord API
How to automatically install Chrome Driver for Chrome version with Python + Selenium + Chrome
Make the Python console covered with UNKO
Get html from element with Python selenium
[Python] Set the graph range with matplotlib
Automatically create Python API documentation with Sphinx
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Behind the flyer: Using Docker with Python
Check the existence of the file with python
[Python] Get the variable name with str
[Python] Round up with just the operator
Let's read the RINEX file with Python ①
Working with OpenStack using the Python SDK
Automatically aggregate JCG deck distribution with Python
Download files on the web with Python
[GCP] Operate Google Cloud Storage with Python
Reboot the router using Python, Selenium, PhantomJS
[Pyto] Operate iPhone Taptic Engine with Python
Operate home appliances with Python and IRKit
Practice web scraping with Python and Selenium
Use selenium phantomjs webdriver with python unittest
Learn the design pattern "Facade" with Python
The road to compiling to Python 3 with Thrift
Python + Selenium + Headless Chromium with aws lambda