This section describes the basic usage when using Selenium with Python.
pip install selenium
When importing with python
from selenium import webdriver
from selenium.webdriver.support.select import Select
#Launch Chrome
driver = webdriver.Chrome()
#Transition to the specified URL
driver.get("Specified URL")
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")
#Press the confirmation dialog
alert = driver.switch_to.alert
alert.accept()
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 an open web browser
driver.quit()
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()
https://tanuhack.com/selenium/#CSSXPath https://www.seleniumqref.com/index.html
Recommended Posts