0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[SikuliX-2.0.5] Selenium を使う

Last updated at Posted at 2022-08-31

SikuliX から Selenium を使うことができます。もはや死角なしです。


準備

SikuliX-2.0.5 は Jython2.7.2 で動作しており、Python2.7 のモジュールを利用できます。
Python2.7 の pipSelenium をダウンロードします。

cmd.exe
c:\Python27>python.exe -m pip download -d src_selenium selenium
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting selenium
  Using cached selenium-3.141.0-py2.py3-none-any.whl (904 kB)
Collecting urllib3
  Downloading urllib3-1.26.12-py2.py3-none-any.whl (140 kB)
     |################################| 140 kB 3.2 MB/s
Saved c:\python27\src_selenium\selenium-3.141.0-py2.py3-none-any.whl
Saved c:\python27\src_selenium\urllib3-1.26.12-py2.py3-none-any.whl
Successfully downloaded selenium urllib3

Python2.7はサポートが終了しているので警告が出ますが、強行します。
ダウンロードされるモジュールとバージョンは次の通りでした。Python3.x 以降でも、pipコマンドで入手できます。

入手した圧縮ファイルからそれぞれモジュールフォルダを取り出し、
フォルダごと%APPDATA%\Sikulix\Lib\site-packages に配備します。

  • selenium-3.141.0-py2.py3-none-any.whl ⇒ selenium を配備
  • urllib3-1.26.12-py2.py3-none-any.whl ⇒ urllib3 を配備

これで SikuliX から使用可能です。

実行

ドライバ類については利用するブラウザに合わせて入手します。
下記のように書くことができますが、SikuliXではなく Selenium の操作としてお調べください。

sample.sikuli
from selenium import webdriver

#Chromeを操作
driver = webdriver.Chrome(executable_path=r"path/to/driver")

driver.open(URL)
#
# 何か操作
#
driver.close()
driver.quit()

初回起動時は問題なく動作しますが、SikuliX IDEを終了せず続けて「実行」したいとき、Jython から urllib3を使用している関係で、ソケットの再接続が必要になります。

実行が2回目以降になると次のエラーが発生しますが…

error
[error] script [ sample ] stopped with error in line 9
[error] java.util.concurrent.RejectedExecutionException 
( java.util.concurrent.RejectedExecutionException: event executor terminated )
[error] --- Traceback --- error source first
line: module ( function ) statement 
919: _socket (  _connect )     bind_future = bootstrap.bind(self.bind_addr).sync()
951: _socket (  connect )     self._connect(addr)
1457: _socket (  meth )     return getattr(self._sock,name)(*args)
1581: _socket (  create_connection )     sock.connect(sa)
1581: _socket (  create_connection )     sock.connect(sa)
106: utils (  is_connectable )     socket_ = socket.create_connection((host, port), 1)
106: utils (  is_connectable )     socket_ = socket.create_connection((host, port), 1)
115: service (  is_connectable )     return utils.is_connectable(self.port)
99: service (  start )     if self.is_connectable():
91: webdriver (  __init__ )     self.iedriver.start()
9: main (  <module> )     driver = webdriver.Ie(executable_path=executable_path)

つぎのようなプログラムを先頭に書いておくことで解消できます。

sample.sikuli
import _socket
if _socket.NIO_GROUP.isShutdown():
    _socket.NIO_GROUP = _socket.NioEventLoopGroup(2, _socket.DaemonThreadFactory("PyScan-Netty-Client-%s"))
    sys.registerCloser(_socket._shutdown_threadpool)

なお、urllib3の関係で初回起動時に下記警告が出ますが、今のところ抑止方法はわかっていません。
Java に疎いので対応方法を教えていただきたい…

warn
log4j:WARN No appenders could be found for logger (io.netty.util.internal.logging.InternalLoggerFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

参考文献

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?