Automate UI testing with Selenium API | Crawling websites with python

environment:

When you want to perform automated tests or operate a website to get something. This time, I used the API of selenium. The documentation is here This time, I used Firefox for crawling. It seems that it can be done with Chrome etc., but it seems that third-party browsers are not supported. .. ..

sample.py


from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys

#Launch browser
driver = webdriver.Firefox()

#Maximize the window
driver.maximize_window()

#Access to the website
driver.get("https://github.com")

#Get the title of the website
driver.title

# "q"Specify an element with the name
#Here, it is a keyword input form for repository search.
form_textfield = driver.find_element_by_name("q")
#Enter "hoge" in the input form
form_textfield.send_keys("hoge")
#Delete the entered content
form_textfield.clear()
#Keyword entered("hoge")And perform a search
form_textfield.submit()
#Find out the location of the input form
form_textfield.location
#Check the size of the input form
form_textfield.size
#Check the tag name of the input form
form_textfield.tag
#Of the element(On HTML source)Examine the text
form_textfield.text

#Specify the element with xpath
#Here, 1 at the bottom of the page, 2, ...,Refers to the button labeled 2 out of the buttons labeled n
next_button = driver.find_element_by_xpath("//*[@id=\"container\"]/div[2]/div/div[2]/div[2]/div/a[1]")
#This element can be clicked, so try clicking it
next_button.click()

#Specify the element with xpath
#Here, I will narrow down to the repository using python and search again.
refined_search_python = driver.find_element_by_xpath("//*[@id=\"container\"]/div[2]/div/div[1]/ul/li[9]/a")
refined_search_python.click()

#Set the timeout period(Page transition) <-I haven't tried it yet
driver.set_page_load_timeout(1)

#Set the timeout period(Script execution) <-I haven't tried it yet
driver.set_page_script_timeout(1)

#Go back one page
#How to do it 1
ActionChains(driver).key_down(Keys.BACKSPACE).send_keys('').keys_up(Keys.BACKSPACE).send_keys('').perform()
#How to do part 2
driver.back()

#Advance one page
driver.forward()

#Take a screenshot
driver.get_screenshot_as_file("./hogehoge.png ")

#Examine the position of the window
driver.get_window_position()

#Check the size of the window
driver.get_window_size()

#Page refresh
driver.refresh()

#Get url
driver.current_url

#Find out what browser you are currently using
driver.name

#Get the source of the currently open page
driver.page_source

#Close browser
#How to do it 1
driver.quit()
#How to do part 2
driver.close()

Recommended Posts

Automate UI testing with Selenium API | Crawling websites with python
Automate python testing with CircleCI
ScreenShot with Selenium (Python)
Scraping with Selenium [Python]
Crawling with Python and Twitter API 1-Simple search function
Automate Chrome with Python and Selenium on your Chromebook
Use Trello API with python
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Use Twitter API with Python
Python: Working with Firefox with selenium
Scraping with Selenium in Python
Web API with Python + Falcon
Play RocketChat with API / Python
Scraping with Selenium + Python Part 2
Call the API with python3.
Use subsonic API with python3
Automate sushi making with Python
Testing with random numbers in Python
Create Awaitable with Python / C API
Automate smartphone app testing with Appium-Python
Get reviews with python googlemap api
Scraping with Selenium in Python (Basic)
Run Rotrics DexArm with python API
Scraping with Python, Selenium and Chromedriver
Quine Post with Qiita API (Python)
Crawling with Python and Twitter API 2-Implementation of user search function
Make testing with Selenium more accessible
Hit the Etherpad-lite API with Python
[python] Read information with Redmine API
Automate simple tasks with Python Part0
Automate Windows Application Testing with Windows Application Driver-Python
Collecting information from Twitter with Python (Twitter API)
[Python] Automate Pelican builds with Travis CI
Get html from element with Python selenium
Automatically create Python API documentation with Sphinx
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Simple Slack API client made with Python
Retrieving food data with Amazon API (Python)
Automate simple tasks with Python Part1 Scraping
[Python] Quickly create an API with Flask
[Python] Automatically operate the browser with Selenium
Serverless face recognition API made with Python
[Python] Get Python package information with PyPI API
Practice web scraping with Python and Selenium
Automate Facebook App Testing with Facebook Test Users
Use selenium phantomjs webdriver with python unittest
Python + Selenium + Headless Chromium with aws lambda
I tried to automate internal operations with Docker, Python and Twitter API + bonus
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
Automatic operation of Chrome with Python + Selenium + pandas
Text extraction with GCP Cloud Vision API (Python3.6)
Try running Google Chrome with Python and Selenium
AtCoder: Python: Automate sample testing as much as possible.
Get stock price data with Quandl API [Python]
LINE BOT with Python + AWS Lambda + API Gateway
Log in to Yahoo Business with Selenium Python
Drag and drop local files with Selenium (Python)
I tried to automate sushi making with python
Explosive speed with Python (Bottle)! Web API development
Recent ranking creation using Qiita API with Python