selenium chrome ubuntu without GUI
时间: 2023-08-30 09:10:46 浏览: 176
To run Selenium with Chrome on Ubuntu without a GUI, you can use the following steps:
1. Install Chrome headless:
```
sudo apt-get update
sudo apt-get install -y xvfb
sudo apt-get install -y unzip
sudo apt-get install -y libxi6 libgconf-2-4
sudo apt-get install -y libglib2.0-0 libnss3 libfontconfig1
sudo apt-get install -y libXtst6
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
```
2. Install ChromeDriver:
```
wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip
unzip chromedriver_linux64.zip
sudo mv chromedriver /usr/bin/chromedriver
sudo chown root:root /usr/bin/chromedriver
sudo chmod +x /usr/bin/chromedriver
```
3. Install Selenium:
```
sudo apt-get install -y python3-pip
sudo pip3 install selenium
```
4. Run Chrome with XVFB:
```
Xvfb :99 -ac -screen 0 1280x1024x16 &
export DISPLAY=:99
```
5. Create a Python script to run Selenium:
```python
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
chrome_options.add_argument("--headless")
driver = webdriver.Chrome(options=chrome_options)
driver.get("https://www.google.com")
print(driver.title)
driver.quit()
```
6. Run the Python script:
```
python3 script.py
```
This will start a headless Chrome browser and open the Google homepage. You can modify the script to navigate to other websites and perform actions with Selenium.