[Python] Specify dynamically changing attributes in Selenium with XPATH (or CSS selector)

Know-how for specifying elements whose attributes change dynamically when scraping.

HTML sample

.html


<input id="sample_123456789">

I think there are many cases where you want to specify such an element every time, but you can specify it dynamically by using XPATH or CSS selector.

Use XPATH

element = driver.find_elements_by_xpath('//*[starts-with(@id,"sample_")]')

If you write it like this, you can specify it dynamically.

Use CSS selector

element = driver.find_elements_by_css_selector("input[id^=sample_]")
element = driver.find_elements_by_css_selector("input[id*=sample_]")

It seems that you can specify it with the CSS selector as well.

Reference link

Element-specific techniques [Selenium] How to get the id that changes dynamically python selenium: iterate through radio buttons that have dynamic ids and select

Recommended Posts

[Python] Specify dynamically changing attributes in Selenium with XPATH (or CSS selector)
Achieve scraping with Python & CSS selector in 1 minute
Scraping with selenium in Python
Scraping with Selenium in Python
Scraping with Selenium in Python (Basic)
CSS parsing with cssutils in Python
[Python / Selenium] XPath
Log in to Yahoo Business with Selenium Python
How to specify attributes with Mock of python
I was addicted to scraping with Selenium (+ Python) in 2020
Pretty print json or yaml with color in python
Get in touch with functional programming in JavaScript or Python 3
ScreenShot with Selenium (Python)
Scraping with Selenium + Python Part 1
Scraping with chromedriver in python
Dynamically import scripts in Python
Debugging with pdb in Python
Python: Working with Firefox with selenium
Working with sounds in Python
Scraping with Tor in Python
Tweet with image in Python
Scraping with Selenium + Python Part 2
Dynamically call methods in Python
Specify python version with virtualenv
[Python] Get elements by specifying attributes with prefix search in BeautifulSoup