Recently, I think that I will summarize in this article the problems that I had in creating a mechanism to automate the input work of selenium. If you write what to import, pycharm will tell you what to import, so omit it
Store the login information of the target site in the user profile with chrome, specify the user profile with selenium, and avoid it by initializing the driver.
# main.py
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("--user-data-dir=C:\Users\Yoshi\AppData\Local\Google\Chrome\User Data")
options.add_argument("--profile-directory=Profile 7")
driverPath = "C:\Program Files (x86)\WebDriver\chromedriver_win32\chromedriver_87.exe"
driver = webdriver.Chrome(executable_path=driverPath, options=options)
Use execute_script to enter a value in the text box.
#Omit web driver initialization
targetId = "targetId"
inputVal = "inputVal"
#When you want to enter a character string including line breaks ↓
# inputVal = inputVal.replace('\n', '\\n')
driver.execute_script(f'document.getElementById("{targetId}").value="{inputVal}";')
#Omit web driver initialization
if len(driver.find_elements(By.ID, "element")) > 0:
print("There is.")
#Omit web driver initialization
#You can click anything that triggers the display with Comform Alert
driver.find_element_by_xpath("xpath").click()
Alert(driver).accept() # OK
Alert(driver).dismiss() #Cancel
#Omit web driver initialization
productImgFilePath = r"C:\work\RakumaAutoApp\1.jpg "
driver.find_element_by_xpath("xpath").send_keys(productImgFilePath)
Refer to the following ↓ https://takazawa.github.io/hobby/selenium_python_tor/
I think that only chrome, maybe other browsers are almost the same ・ Open the developer screen with F12
・ Choose anything You can find it by looking at the right click of the target element and Copy.
It was a good study to touch Python for the first time when I created a program for automating input work. It's nice that Selenium can do something by moving the browser without doing anything.
Recommended Posts