Operate Firefox with Selenium from python and save the screen capture

procedure

Environmental preparation

python


sudo easy_install selenium

Export from Selenium IDE

  1. Create a test case with Selenium IDE.

スクリーンショット 2014-08-28 6.09.55.png

  1. Select Python 2 / unitetest / WebDriver in Export

スクリーンショット 2014-08-28 6.10.35.png

  1. Save as selenium.py

Fixed selenium.py

Screen capture is not possible as it is.

ERROR: Caught exception [ERROR: Unsupported command [captureEntirePageScreenshot | /Users/owner/a.jpg | ]]

Add the following under the line

python


        driver.save_screenshot("a.jpg ")

Reference: Seleniumworks: Capture the Screen on Test failure --WebDriver

Run

python


python selenium.py

Confirm that Firefox works and a.jpg is generated.

Reference: Full text of selenium.py

selenium.py


# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Aa(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://auction.yahoo.co.jp/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_aa(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_link_text(u"My auction").click()
        driver.find_element_by_link_text(u"help").click()
        driver.find_element_by_link_text(u""The ID or password is incorrect" is displayed").click()
        # ERROR: Caught exception [ERROR: Unsupported command [captureEntirePageScreenshot | /Users/owner/a.jpg | ]]
        driver.save_screenshot("a.jpg ")

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

Support for basic authentication

selenium.py


- self.base_url = "http://secret.page.local/"
+ self.base_url = "http://username:[email protected]/"

xfce4 desktop icon support

google.selenium


#!/bin/sh
python google.py

Prepare something like the above and double-click to execute it.

Prevent firefox from quitting automatically

google.py


+   #def tearDown(self):
+       #self.driver.quit()
+       #self.assertEqual([], self.verificationErrors)

Recommended Posts

Operate Firefox with Selenium from python and save the screen capture
Python: Working with Firefox with selenium
Scraping with Python, Selenium and Chromedriver
Get web screen capture with python
Operate Jupyter with REST API to extract and save Python code
[Python3] Save the mean and covariance matrix in json with pandas
Get html from element with Python selenium
Operate home appliances with Python and IRKit
Practice web scraping with Python and Selenium
Fill the string with zeros in python and count some characters from the string
[python] Send the image captured from the webcam to the server and save it
Install Chrome on the command line on Sakura VPS (Ubuntu) and launch Chrome with python from virtual display and selenium
Try running Google Chrome with Python and Selenium
Drag and drop local files with Selenium (Python)
Operate the schedule app using python from iphone
Archive and compress the entire directory with python
Deep Learning from scratch The theory and implementation of deep learning learned with Python Chapter 3
Pulling songwriting, composition and arrangement information from the Tower Records site with Python
Try out the touch of data-driven testing with Selenium Python Bindings and py.test
Learn Nim with Python (from the beginning of the year).
Use FireFox (via Homebrew-cask) with Selenium WebDriver Python bindings
Install selenium on Mac and try it with python
Try to operate DB with Python and visualize with d3
Extract images and tables from pdf with python to reduce the burden of reporting
I tried to automate the article update of Livedoor blog with Python and selenium.
Visualize the range of interpolation and extrapolation with python
Operate Kinesis with Python
Automatic follow on Twitter with python and selenium! (RPA)
Operate neutron from Python!
ScreenShot with Selenium (Python)
Operate Blender with Python
Operate LXC from Python
Install the latest stable Python with pyenv (both 2 and 3)
Get mail from Gmail and label it with Python3
Scraping with Selenium [Python]
Automate Chrome with Python and Selenium on your Chromebook
Install the latest Python from pyenv installed with homebrew
Automatically translate DeepL into English with Python and Selenium
Operate Excel with Python (1)
Run iphone safari from mac with python + selenium + safari-webdriver
How to operate Firefox with selenium on Windows Memo
Operate Excel with Python (2)
I want to cut out only the face from a person image with Python and save it ~ Face detection and trimming with face_recognition ~
Shuffle the images in any directory with Python and save them in another folder with serial numbers.
Put Ubuntu in Raspi, put Docker on it, and control GPIO with python from the container
Save the input video from the capture board as an image
Try hitting the Twitter API quickly and easily with Python
screen and split screen with python and ssh login to remote server
I tried using the Python library from Ruby with PyCall
Solve the spiral book (algorithm and data structure) with python!
Recognize your boss and hide the screen with Deep Learning
[Python] I installed the game from pip and played it
Hash with python and escape from a certain minister's egosa
Save images on the web to Drive with Python (Colab)
Operate your browser using the Selenium Web Driver Python bindings
Play with the password mechanism of GitHub Webhook and Python
Programming with Python and Tkinter
Operate Excel with Python openpyxl
Encryption and decryption with Python
Operate TwitterBot with Lambda, Python
Scraping with selenium in Python