linux下使用selenium


安裝chromedriver

1、安裝chrome

用下面的命令安裝最新的 Google Chrome

yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
也可以下載到本地再安裝
wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
yum install ./google-chrome-stable_current_x86_64.rpm 

 

安裝必要的庫
yum install mesa-libOSMesa-devel gnu-free-sans-fonts wqy-zenhei-fonts

 

2、安裝 chromedriver

查看谷歌瀏覽器版本 

(base) [root@brady ~]# google-chrome --version                                  
Google Chrome 78.0.3904.108

  

chrome官網 wget https://chromedriver.storage.googleapis.com/2.38/chromedriver_linux64.zip
淘寶源(推薦)
wget http://npm.taobao.org/mirrors/chromedriver/2.41/chromedriver_linux64.zip

 

 

將下載的文件解壓,放在如下位置

unzip chromedriver_linux64.zip

/usr/bin/chromedriver

給予執行權限

chmod +x /usr/bin/chromedriver


還可以參考這篇文章,雖然沒試過,但感覺很牛逼 

 https://intoli.com/blog/installing-google-chrome-on-centos/

 

解決selenium報錯--unknown error: DevToolsActivePort file doesn't exist

 

早上在linux下用selenium啟動Chrome時出現問題:
報錯:

Traceback (most recent call last): File "get2.py", line 62, in <module> browser = webdriver.Chrome() File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py", line 75, in __init__ desired_capabilities=desired_capabilities) File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 154, in __init__ self.start_session(desired_capabilities, browser_profile) File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 243, in start_session response = self.execute(Command.NEW_SESSION, parameters) File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 311, in execute self.error_handler.check_response(response) File "/root/.pyenv/versions/anaconda3-5.1.0/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 237, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (unknown error: DevToolsActivePort file doesn't exist) (The process started from chrome location /usr/bin/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.) (Driver info: chromedriver=2.43.600233 (523efee95e3d68b8719b3a1c83051aa63aa6b10d),platform=Linux 4.9.0-6-amd64 x86_64) 

 

 

解決方案:
加上代碼:

from selenium.webdriver.chrome.options import Options chrome_options = Options() chrome_options.add_argument('--no-sandbox') chrome_options.add_argument('--disable-dev-shm-usage') chrome_options.add_argument('--headless') browser = webdriver.Chrome(chrome_options=chrome_options) 

 

其中
“–no-sandbox”參數是讓Chrome在root權限下跑
“–headless”參數是不用打開圖形界面
可以額外加這些參數獲得更好體驗

chrome_options.add_argument('blink-settings=imagesEnabled=false') chrome_options.add_argument('--disable-gpu')


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM