Can Selenium automate the browser?

Purpose

Equipment with a Web management screen has an authentication screen. I want to automate it somehow. Projectors, webcams, and more.

There seems to be something called Selenium

Continue reading the Selenium Browser Automation Project (https://www.selenium.dev/documentation/ja/).

Put Ruby

Install the latest version from Ruby Installer for windows.

I referred to [Beginner] From Ruby installation to simple program execution.

Put the library

Run it with cmd.exe.

gem install selenium-webdriver

Insert WebDriver

download

If you think you can do it from the Chrome download from the Quick Reference in Driver Requirements (https://www.selenium.dev/documentation/ja/webdriver/driver_requirements/), you'll see a list of version folders. The number is the latest, so open the folder and download "chromedriver_win32.zip".

Installation

Unzip the zip and save "chromedriver.exe" in C: \ WebDriver \ bin.

pass through path

setx /m path "%path%;C:\WebDriver\bin\"

Reboot the OS and try

>chromedriver
Starting ChromeDriver 88.0.4324.27 (6347fe8bf1e48bd0c54d07dc55ca011cf40861c9-refs/branch-heads/4324@{#450}) on port 9515
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.

try!

Create a trial file.

selenium.rb


require "selenium-webdriver"

driver = Selenium::WebDriver.for :chrome

Run the trial file.

>ruby 20201217001.rb
Traceback (most recent call last):
        2: from 20201217001.rb:1:in `<main>'
        1: from C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- selenium-webdriver (LoadError)
(Omitted)
x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require'
C:/Ruby27-x64/lib/ruby/2.7.0/rubygems/core_ext/kernel_require.rb:92:in `require': cannot load such file -- ffi (LoadError)
(Omitted)
3.0.0/lib/childprocess/windows.rb:3:in `<top (required)>'
C:/Ruby27-x64/lib/ruby/gems/2.7.0/gems/childprocess-3.0.0/lib/childprocess/windows.rb:6:in `rescue in <top (required)>': FFI is a required pre-requisite for Windows or posix_spawn support in the ChildProcess gem. Ensure the `ffi` gem is installed. If you believe this is an error, please file a bug at http://github.com/enkessler/childprocess/issues (ChildProcess::MissingFFIError)

I got an error.

It's easy to get caught up in the first "` require': cannot load such file --selenium-webdriver (LoadError) ". The "ffi" that appeared in the middle is like a point. Or maybe the Q & A site hasn't pasted the error, so it hasn't been resolved.

Additional installation

I'm overconfident that this is the correct answer for the QA site in the world.

gem install ffi

Try 2!

This time should be fine.

>ruby 20201217001.rb
DevTools listening on ws://127.0.0.1:49937/devtools/browser/2ac6997d-7b76-4230-b56a-b05765457035
Traceback (most recent call last):
(Omitted)
Backtrace:: session not created: This version of ChromeDriver only supports Chrome version 88 (Selenium::WebDriver::Error::SessionNotCreatedError)
Current browser version is 87.0.4280.88 with binary path C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe

that? It seems that the browser and WebDriver versions do not match. It seems that the latest is not good. Download the same version from the driver requirements and replace it with C: \ WebDriver \ bin.

>ruby 20201217001.rb

DevTools listening on ws://127.0.0.1:50041/devtools/browser/a4f2bc4b-2ead-48bc-9af6-db8329221104

It seems to have succeeded. (Really?)

Experiment

It's different from trying.

Open browser

require "selenium-webdriver"
driver = Selenium::WebDriver.for :chrome
#Open URL
#driver.get 'https://selenium.dev'

In a blink of an eye, why? The browser closes. It seems that the driver needs a closing or closing command.

Measures to close without permission

#sleep(5)

Keep it a little if you add it to the end. But I stopped with wait.

Basic authentication

How to access Basic authentication site with Selenium Webdriver

driver.get 'http://admin:[email protected]/basic_auth'

Here, a voice came out without permission, "Wow!"

Wait to be displayed

The page is displayed after authentication. I want to get rid of the code looking for an element before it's displayed.

Try inserting Wait with Selenium Webdriver

#Variable declaration that gives wait a 60-second timer
wait = Selenium::WebDriver::Wait.new(:timeout => 60)

#Matsu until it is displayed
wait.until{driver.find_element(:id, 'pst_select').displayed?}

It was a frame

The element cannot be found by any means. Frame nested orz

The operation of the frame does not work. I decided to open the frame directly.

Select select tag

Specify a value and click.

#Specify element
element = driver.find_element(:id, 'pst_select')
#Select tag specification
select = Selenium::WebDriver::Support::Select.new(element)

#Value specification
select.select_by(:value, '1')
#Click an element
driver.find_element(:id, 'pst_move').click

Final result

I want to operate multiple devices, so I made a loop.

cam_reset.rb


require "selenium-webdriver"

roomNum = {	"001" => "10.1.1.1",
			"002" => "10.1.1.2"}

roomNum.each{|room, ipAddress|
	driver = Selenium::WebDriver.for :chrome

	#Open URL
	camUrl = "http://account:password@" + ipAddress + "/live/live_flame_ctrl.html"
	driver.get camUrl

	#Variable declaration that gives wait a 60-second timer
	wait = Selenium::WebDriver::Wait.new(:timeout => 60)

	#Matsu until it is displayed
	wait.until{driver.find_element(:id, 'pst_select').displayed?}

	#Specify element
	element = driver.find_element(:id, 'pst_select')
	#Select tag specification
	select = Selenium::WebDriver::Support::Select.new(element)

	#Value specification
	select.select_by(:value, '1')
	#Click an element
	driver.find_element(:id, 'pst_move').click
	
	#close
	driver.close
}

All you have to do is create a batch file.

camreset.bat


ruby cam_reset.rb

Recommended Posts

Can Selenium automate the browser?
Mechanism for converting to a language that the browser can recognize
[Linux: Ruby] Use the driver with selenium without specifying the browser driver path
Follow the link with Selenium (Java)
Check the root on the Rails browser