Ali cloud Ubuntu server using selenium chrome + headless (without a head - no interface)

Ali cloud using ubuntu server using selenium automation reptiles, need to install Google browser (also may be other browsers) and the corresponding version of the driver, and selenium need to configure the headless, no-sandbox and so on.

1. Install selenium

pip install selenium

2. Install the Google browser

sudo apt-get install libxss1 libappindicator1 libindicator7
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome*.deb    # Might show "errors", fixed by next line
sudo apt-get install -f#安装依赖
google-chrome --version      # 查看版本

3. Install chromdriver

Enter Ali cloud images download chromdriver
Mirror Image Source
download above corresponding Google browser corresponding chromdriver. Usually download the latest.
You can view notes.txt file to see both chrome and ChromDriver corresponding compatible version
Google Chrome
download chromedriver_linux64.zip

Unpack files chromedriver

Chromedirver files on the remote server online directory / usr / bin / under.
If the message permission denied error

During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “amac_project_msg.py”, line 7, in <module>
browser = webdriver.Chrome(executable_path="/usr/local/bin/chromedriver", chrome_options=opt)
File “/root/anaconda3/envs/opinion/lib/python3.5/site-packages/selenium/webdriver/chrome/webdriver.py”, line 73, in init
self.service.start()
File “/root/anaconda3/envs/opinion/lib/python3.5/site-packages/selenium/webdriver/common/service.py”, line 88, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: ‘chromedriver’ executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home

The solution is to provide the right chromedriver in the path.

chmod 777 chromedriver

4. Test

from selenium import webdriver
 
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')#无头模式,服务器没有图形界面这个必须
chrome_options.add_argument('--disable-gpu')#不需要gpu加速
chrome_options.add_argument('--no-sandbox') # 这个配置很重要
client = webdriver.Chrome(chrome_options=chrome_options, executable_path='/home/chromedriver')    # 如果没有把chromedriver加入到PATH中,就需要指明路径
 
client.get("https://www.baidu.com")
print (client.page_source.encode('utf-8'))
 
client.quit()

Successful print out the page content, it is ok! ! ! !

Released five original articles · won praise 5 · Views 248

Guess you like

Origin blog.csdn.net/weixin_44388092/article/details/104220500