I want to automatically operate Chrome on Windows from a virtual machine (ubuntu) started using Vagrant

Thing you want to do

Remotely access Chrome on the host (Windows 10 running the virtual machine) from Selenium (python) on the virtual machine (ubuntu) started using Vagrant.

environment

Virtual machine side
Host side

Problematic operation

Premise
operation
    1. Run the following code in Windows Power Shell on the host (with chromedriver in the current directory)
.\chromedriver.exe --port=4444
    1. Execute the following python code on vagrant.

test.py


from selenium.webdriver import Chrome, ChromeOptions, Remote

options = ChromeOptions()
driver = Remote('http://10.0.2.2:4444', options=options)
#Omitted below
    1. The following error is output
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

Cause

The chromedriver default settings only allow local connections.

Countermeasures

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!

reference

The method introduced is described in the official chromedriver documentation.

Knowledge about Selenium is available at https://www.selenium.dev/documentation/ja/

Recommended Posts

I want to automatically operate Chrome on Windows from a virtual machine (ubuntu) started using Vagrant
Create a Linux virtual machine on Windows
I want to operate DB using Django's ORM from an external application
I want to email from Gmail using Python.
I want to do pyenv + pipenv on Windows
Environment construction procedure to operate chrome without installing python on Windows (using selenium)
I want to take a screenshot of the site on Docker using any font
I want to use OpenJDK 11 on Ubuntu Linux 18.04 LTS / 18.10
I want to send a business start email automatically
I want to do Wake On LAN fully automatically
How to build a Python virtual execution environment using Visual Studio Code and pipenv on a Windows machine (also Jupyter notebook)
How to build a new python virtual environment on Ubuntu
I want to start a lot of processes from python
I want to automatically generate a modern metal band name
I want to use a virtual environment with jupyter notebook!
I want to install a package from requirements.txt with poetry
I want to send a message from Python to LINE Bot
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Operate ubuntu on VScode (windows10)
I tried to create a server environment that runs on Windows 10
I want to automatically find high-quality parts from the videos I shot
I just wrote a script to build Android on another machine
How to build a Python environment using Virtualenv on Ubuntu 18.04 LTS
I tried installing MySQL on a Linux virtual machine on OCI Compute
I want to make a parameter list from CloudFormation code (yaml)
I want to create a machine learning service without programming! WebAPI
I tried playing Windows games using Steam Play on Ubuntu 20.04 LTS
Everything from building a Python environment to running it on Windows
Process and result of connecting to SQL Server on windows from Linux virtual environment using JDBC driver
Using a serial console on Ubuntu 20.04
I want to pass an argument to a python function and execute it from PHP on a web server
Aiming to become a machine learning engineer from sales positions using MOOCs
I want to send a signal only from the sub thread to the main thread
[Python memo] I want to get a 2-digit hexadecimal number from a decimal number
Create a tool to automatically furigana with html using Mecab from Python3
I want to display an image on Jupyter Notebook using OpenCV (mac)
I want to create an Ubuntu chrome User Profile with Colab only
I want to create a machine learning service without programming! Text classification
I want to make a web application using React and Python flask
I want to print in a comprehension
I want to use jar from python
I want to build a Python environment
I want to use Linux on mac
Creating a python virtual environment on Windows
I built a TensorFlow environment on windows10
I tried using Headless Chrome from Selenium
I started Node.js in a virtual environment
I want to develop Android apps on Android
[LINE Messaging API] I want to send a message from the program to everyone's LINE
I want to use Python in the environment of pyenv + pipenv on Windows 10
I made a library to operate AWS CloudFormation stack from CUI (Python Fabric)
I just want to add scipy, but it's a messy note. Ubuntu, Python 3.
I want to drop a file on tkinter and get its path [Tkinter DnD2]
I tried to automatically generate a port management table from Config of L2SW
[Azure] I tried to create a Linux virtual machine in Azure of Microsoft Learn
I tried to make a translation BOT that works on Discord using googletrans
I want to do machine learning even without a server --Time Series Edition -
I want to realize something like AutoHotkey with AutoKey on Ubuntu (Kali Linux)
What to do when you want to receive files from a Windows client remotely