Remotely access Chrome on the host (Windows 10 running the virtual machine) from Selenium (python) on the virtual machine (ubuntu) started using Vagrant.
vagrant up
..\chromedriver.exe --port=4444
test.py
from selenium.webdriver import Chrome, ChromeOptions, Remote
options = ChromeOptions()
driver = Remote('http://10.0.2.2:4444', options=options)
#Omitted below
Traceback (most recent call last):
File "test.py", line 4, in <module>
driver = Remote('http://10.0.2.2:4444', options=options)
#Omission
File "/home/vagrant/scraping/lib/python3.8/site-packages/selenium/webdriver/remote/errorhandler.py", line
208, in check_response
raise exception_class(value)
selenium.common.exceptions.WebDriverException: Message: Host header or origin header is specified and is not whitelisted or localhost.
In addition, the following log is spit out in the chrome driver running on the host side.
[1610458155.162][SEVERE]: Rejecting request with host: 10.0.2.2:4444. origin is
The chromedriver default settings only allow local connections.
To allow connections from remote hosts, run chromedriver You can add --allowed-ips to the command.
.\chromedriver.exe --port=4444 --allowed-ips
If you execute the python code on the virtual machine again while the chrome driver is running on the host side, you can automatically operate Chrome on Windows from ubuntu on the virtual machine!
The method introduced is described in the official chromedriver documentation.
Knowledge about Selenium is available at https://www.selenium.dev/documentation/ja/
Recommended Posts