[PYTHON] Beginning with Selenium

I touched Selenium for the time being, so I wrote a little note. However, I haven't tried all of them, so there are many shortcomings.

What is Selenium in the first place?

A tool for testing websites. By writing a script in advance, it is possible to perform the exact operation on the browser and check whether the element exists in the middle and whether the URL matches the expected value.

For the time being, I heard a story like "Selenium when it comes to website testing", so when I googled it, I was very worried because the article was just old, but around 2012, the 2.x series It seems that the product has been released and can still be used.

So, to use Selenium

For the time being, you can download it from the ** English version ** Selenium site. The Selenium site itself has a Japanese version, but you can download 1.x from the Japanese site. First of all, I was really into it here. No matter how much "please see the official website for the latest version" ...

For the time being, place each file in the following location.

In addition, Python here seems to be useless unless it is Version 2 system (3.x does not work properly because there are incompatible parts in various places). I downloaded it from Python official page, but it includes setuptools and pip (necessary for installing Selenium Client). Since there is no such thing, install it referring to the following site.

For the time being, try using it lightly

The easiest way to use it is to start Selenium IDE and record → play the operation.

  1. Start Firefox and start Selenium IDE
  2. Make sure that "Action> Record Test" in Selenium IDE is checked.
  3. Return to the Firefox window and open the site you want to test
  4. Follow the link to the page you want to test
  5. Uncheck "Action> Record Test" to stop recording

Roughly like this, you will have a simple operation code. Selenium IDEウィンドウ

After that, select the line you want to test and add a test by entering a command such as "Insert command" in the right-click menu and "assertTitle" in the command. At this time, note that the expected value must be entered in the "Target" field, not the "Value" field (I was addicted to it again). assertコマンドの指定

Another point to note is that if you want to wait until the page transition is completed after clicking, use the clickAndWait command instead of the click command, and wait for the screen switching process such as Ajax without screen transition. In that case, you can just check the existence of the element by using the waitForVisible command or the like.

Output test case

If you want to output the test cases to a file, select Save Test Cases or Export Test Cases from the File menu. If you choose to save test cases, you will get an HTML file with table tags, one test code per line.

If you choose to export, you can specify the language and save the test code to a file. By default, you can select from 4 languages: python, ruby, c #, java, but if you don't like the code format or want to output in another language, you can add it from "Options> Settings".

Run the exported test case

The exported test cases can be run using their respective interpreters as usual (provided that the Selenium client module for each language is pre-installed). However, as mentioned above, when using Python, it is necessary to have version 2 series. Please note that the script does not work well with 3 series.

Also, in my environment, I couldn't use the file output by Ruby because of a syntax error. I haven't tried C # etc. because it is troublesome to compile.

Test cases exported with "Remote Control" written after the language name will be processed by starting any browser using Selenium Server, so it is necessary to start Selenium Server in advance. See the following site for how to start Selenium Server.

For the time being, it looks like this. If I find something again, I may add it.

Recommended Posts

Beginning with Selenium
Scraping with selenium ~ 2 ~
Scraping with Selenium
Beginning with PyTorch
Successful scraping with Selenium
ScreenShot with Selenium (Python)
Scraping with Selenium [Python]
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Python: Working with Firefox with selenium
selenium
Scraping with Selenium in Python
Memories of fighting with Selenium
Scraping with Selenium + Python Part 2
Try Selenium Grid with Docker
Beginning with Python machine learning
I-town page scraping with selenium
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
Make testing with Selenium more accessible
Get html from element with Python selenium
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
selenium: wait for element with AND / OR
I tried using Selenium with Headless chrome
Automat WEB process with Heroku + Selenium + Chrome
[Python] Automatically operate the browser with Selenium
Practice web scraping with Python and Selenium
Build a capture acquisition machine with Selenium
Use selenium phantomjs webdriver with python unittest
I really wanted to copy with selenium
Python + Selenium + Headless Chromium with aws lambda
Download PDF with selenium chromedriver (Chrome 57) + (Other notes)
Automatic operation of Chrome with Python + Selenium + pandas
Try running Google Chrome with Python and Selenium
Serverless scraping using selenium with [AWS Lambda] -Part 1-
How to deal with SessionNotCreatedException when using Selenium
Log in to Yahoo Business with Selenium Python
Drag and drop local files with Selenium (Python)
[EC2] Prevent garbled characters when capturing with selenium
Automatic login to ServiceNow with Selenium Web Driver