Code etc. that beginners referred to when they stumbled when using WebDriver with Selenium

You cannot click elements that are not on the screen

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")

Scroll to bring it to the screen


driver.set_window_size(xxxx,yyyy)

First change the screen size with. I set the screen size to 1980 and 1280 because it is troublesome to scroll every time. It seems that you can scroll to other elements such as ID.


Some sites treat it as a robot without a user agent.

options.add_argument(f'user-agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.79 Safari/537.36')

Impersonate a user agent.


If you can't click Javascript buttons, etc.

elements = driver.find_element_by_xpath("XPATH")
loc = elements.location
x, y = loc['x'], loc['y']
actions = ActionChains(driver)
actions.move_by_offset(x, y)
actions.click()
actions.perform()

Click on the XPATH coordinates.


If the text is not on the screen, it cannot be obtained with .text etc.

text = driver.find_element_by_id("ID").get_attribute("textContent")

Get with get_attribute.

end

It's been a month since I started, so there may be some rumors that this is different, but I would appreciate it if you kindly say it.

Recommended Posts

Code etc. that beginners referred to when they stumbled when using WebDriver with Selenium
How to deal with SessionNotCreatedException when using Selenium
How to not load images when using PhantomJS with Selenium
Where to fix code when using plotly for free
A story that stumbled when I made a chatbot with Transformer
Eliminate errors that occur when using Django REST Swagger with Django 3.0
Code snippets often used when using BigQuery with Google Colab
A story that stumbled when using pip in a proxy environment
How to resolve CSRF Protection when using AngularJS with Django
Note links that may be useful when using Python, Selenium2
Things to keep in mind when using Python with AtCoder
Things to keep in mind when using cgi with python.
How to update FC2 blog etc. using XMLRPC with python
[Python3] List of sites that I referred to when I started Python