selenium case study summary python pyCharm

Summary of problems with Selenium

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

Avoid reCAPTCHA and two-step verification

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)

Send.key is slow ...

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}";')

Check the existence of the element

#Omit web driver initialization
if len(driver.find_elements(By.ID, "element")) > 0:
    print("There is.")

Press the OK button on the Conform Alert box

#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

Send image to type file

#Omit web driver initialization
productImgFilePath = r"C:\work\RakumaAutoApp\1.jpg "
driver.find_element_by_xpath("xpath").send_keys(productImgFilePath)

I want to change the IP address for each access

Refer to the following ↓ https://takazawa.github.io/hobby/selenium_python_tor/

How to check xpath

I think that only chrome, maybe other browsers are almost the same ・ Open the developer screen with F12 image.png

・ Choose anything You can find it by looking at the right click of the target element and Copy. image.png

Summary

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

selenium case study summary python pyCharm
python pandas study recent summary
Python summary
Python + Selenium Frequently used operation method summary
Python study note_002
Python study notes _000
Python tutorial summary
Python study notes_006
Python study note_004
Python study note_003
Python study notes _005
python related summary
Python study notes_001
Python basics summary
Python study day 1
Summary about Python scraping
Python Django tutorial summary
case class in python
Programming study site summary
Summary about Python3 + OpenCV3
Python function argument summary
Python directory operation summary
ScreenShot with Selenium (Python)
Python AI framework summary
Python iteration related summary
Summary of Python arguments
Scraping with Selenium [Python]
Python web scraping selenium
python selenium chromedriver beautifulsoup
Python + Anaconda + Pycharm environment construction
Python3 programming functions personal summary
Run python with PyCharm (Windows)
Scraping with selenium in Python
Summary of python file operations
Summary of Python3 list operations
What's new in Python 3.10 (Summary)
Study Python with Google Colaboratory
Python: Working with Firefox with selenium
Python Selenium Dynamic download wait
Study from Python Hour4: Object-oriented ②
Standard input / summary / python, ruby
Scraping with Selenium in Python
Study from Python Hour3: Functions
Python class member scope summary
Python web programming article summary
Start to Selenium using python
Selenium WebDriver + Firefox49 (provisional) (Python)
Web scraping using Selenium (Python)
Python Competitive Programming Site Summary
[WIP] Fluent Python Study Note
Python data type summary memo
Study from Python Hour4: Object-oriented ①
Face detection summary in Python
Scraping with Selenium + Python Part 2
[Python + Selenium] Tips for scraping
What's new in Python 3.9 (Summary)
Use Python on Windows (PyCharm)
Challenge Python3 and Selenium Webdriver
Python Crawling & Scraping Chapter 4 Summary