💬

selenium IDEからPython実行コードをエクスポートして実行するまで

2023/05/16に公開

前提

IDEを動かす環境:Windows10
seleniumを動かす環境:WSL

以下でseleniumIDEを取得する。 ブラウザもここに入ってるので、FirefoxやChromeにプラグインを入れる必要はない。
https://github.com/SeleniumHQ/selenium-ide/releases/tag/4.0.0-alpha.14

環境構築

pip install selenium==4.0
vi /etc/yum.repos.d/google-chrome.repo

以下を入力

[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=0
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub

インストール

yum install --enablerepo=google-chrome google-chrome-stable
google-chrome --version
wget https://chromedriver.storage.googleapis.com/107.0.5304.62/chromedriver_linux64.zip

IDE起動

test case 作成

以下のようなファイルが出力される。

test_googleabout.py
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestGoogleabout():
  def setup_method(self, method):
    self.driver = webdriver.Chrome()
    self.vars = {}
  
  def teardown_method(self, method):
    self.driver.quit()
  
  def test_googleabout(self):
    self.driver.get("https://www.google.com/")
    self.driver.set_window_size(1034, 824)
    self.driver.find_element(By.LINK_TEXT, "Googleについて").click()
  

pytest [モジュール名]で実行します



実行結果

$ pytest sel_google.py
================================================ test session starts ================================================
platform linux -- Python 3.7.6, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /root
plugins: celery-4.4.7, django-4.1.0
collected 1 item

sel_google.py E [100%]

====================================================== ERRORS =======================================================
________________________________ ERROR at setup of TestGoogleabout.test_googleabout _________________________________

self = <sel_google.TestGoogleabout object at 0x7f34b8771750>
method = <bound method TestGoogleabout.test_googleabout of <sel_google.TestGoogleabout object at 0x7f34b8771750>>

def setup_method(self, method):
self.driver = webdriver.Chrome()

:
:
E selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally.
E (unknown error: DevToolsActivePort file doesn't exist)
E (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
:
:
============================================== short test summary info ==============================================
ERROR sel_google.py::TestGoogleabout::test_googleabout - selenium.common.exceptions.WebDriverException: Message: u...
================================================= 1 error in 1.17s =================================================



```python:
def setup_method(self, method):
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    self.driver = webdriver.Chrome(options=options)
    self.vars = {}
test_googleabout.py
# Generated by Selenium IDE
import pytest
import time
import json
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities

class TestGoogleabout():
  def setup_method(self, method):
    options = webdriver.ChromeOptions()
    options.add_argument('--headless')
    options.add_argument('--no-sandbox')
    options.add_argument('--disable-gpu')
    self.driver = webdriver.Chrome(options=options)
    self.vars = {}

  def teardown_method(self, method):
    self.driver.quit()

  def test_googleabout(self):
    self.driver.get("https://www.google.com/")
    self.driver.set_window_size(1034, 824)
    self.driver.find_element(By.LINK_TEXT, "Googleについて").click()

実行結果

$ pytest sel_google.py
====================================== test session starts =======================================
platform linux -- Python 3.7.6, pytest-6.1.2, py-1.9.0, pluggy-0.13.1
rootdir: /root
plugins: celery-4.4.7, django-4.1.0
collected 1 item

sel_google.py .                                                                            [100%]

======================================= 1 passed in 2.86s ========================================

参考
https://blog.denet.co.jp/centos7-python-selenium/#CentOS7でPythonSeleniumChromeの動作環境の作成

Discussion