如何让硒和铬在 ubuntu 上工作
how to make selenium and chromium work on ubuntu
我正在尝试将 selenium 与 Chrome 结合使用来浏览使用 javascript.
的网站
我安装了:
铬:
sudo apt-get install chromium-browser
chrome 驱动程序,从 https://sites.google.com/a/chromium.org/chromedriver/downloads
下载并解压
硒
pip install selenium
但是,我无法完成这项工作:
from selenium import webdriver
service_args = ['--verbose']
service_log_path = "{}/chromedriver.log".format('/projects/')
driver = webdriver.Chrome('/path/to/chromedriver', service_args=service_args, service_log_path=service_log_path)
我收到这个错误:
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.13.307649 (bf55b442bb6b5c923249dd7870d6a107678bfbb6),platform=Linux 3.13.0-30-generic x86_64)
这是我的日志文件:
什么是开发工具问题?
[0.973][INFO]: COMMAND InitSession {
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions": {
"args": [ ],
"extensions": [ ]
},
"javascriptEnabled": true,
"platform": "ANY",
"version": ""
}
}
[0.980][INFO]: Populating Preferences file: {
"alternate_error_pages": {
"enabled": false
},
"autofill": {
"enabled": false
},
"browser": {
"check_default_browser": false
},
"distribution": {
"import_bookmarks": false,
"import_history": false,
"import_search_engine": false,
"make_chrome_default_for_user": false,
"show_welcome_page": false,
"skip_first_run_ui": true
},
"dns_prefetching": {
"enabled": false
},
"profile": {
"content_settings": {
"pattern_pairs": {
"https://*,*": {
"media-stream": {
"audio": "Default",
"video": "Default"
}
}
}
},
"default_content_settings": {
"geolocation": 1,
"mouselock": 1,
"notifications": 1,
"popups": 1,
"ppapi-broker": 1
},
"password_manager_enabled": false
},
"safebrowsing": {
"enabled": false
},
"search": {
"suggest_enabled": false
},
"translate": {
"enabled": false
}
}
[0.982][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
}
[0.988][INFO]: Launching chrome: /usr/bin/chromium-browser --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.Wgxcag/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12948 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.Fqyvg3 data:,
[0.991][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[0.999][WARNING]: PAC support disabled because there is no system implementation
[1.113][DEBUG]: DevTools request failed
[1.165][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.166][DEBUG]: DevTools request failed
[1.217][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.217][DEBUG]: DevTools request failed
[1.268][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.269][DEBUG]: DevTools request failed
[1.319][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.320][DEBUG]: DevTools request failed
[1.370][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.371][DEBUG]: DevTools request failed
[1.422][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.423][DEBUG]: DevTools request failed
[1.474][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.475][DEBUG]: DevTools request failed
[1.526][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.527][DEBUG]: DevTools request failed
[1.577][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.578][DEBUG]: DevTools request failed
[1:1:0123/165812:ERROR:image_metadata_extractor.cc(111)] Couldn't load libexif.
[1.629][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.630][DEBUG]: DevTools request failed
[6845:6845:0123/165812:ERROR:browser_main_loop.cc(209)] Gtk: cannot open display:
[1.680][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.681][DEBUG]: DevTools request failed
[1.731][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.735][DEBUG]: DevTools request failed
[1.786][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.787][DEBUG]: DevTools request failed
[1.837][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.838][DEBUG]: DevTools request failed
[1.889][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.891][DEBUG]: DevTools request failed
[1.942][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.944][DEBUG]: DevTools request failed
[1.995][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.997][DEBUG]: DevTools request failed
您可以在虚拟显示器上使用 Xvfb
with the help of pyvirtualdisplay
到 运行 您的 chrome 自动化,因为您没有真实的显示器:
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
另请参阅:
- How do I run Selenium in Xvfb?
除此之外,切换到 PhantomJS
也是一种选择:
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get('http://www.google.com')
print browser.title
browser.quit()
我正在尝试将 selenium 与 Chrome 结合使用来浏览使用 javascript.
的网站我安装了:
铬:
sudo apt-get install chromium-browser
chrome 驱动程序,从 https://sites.google.com/a/chromium.org/chromedriver/downloads
下载并解压
硒
pip install selenium
但是,我无法完成这项工作:
from selenium import webdriver
service_args = ['--verbose']
service_log_path = "{}/chromedriver.log".format('/projects/')
driver = webdriver.Chrome('/path/to/chromedriver', service_args=service_args, service_log_path=service_log_path)
我收到这个错误:
WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(Driver info: chromedriver=2.13.307649 (bf55b442bb6b5c923249dd7870d6a107678bfbb6),platform=Linux 3.13.0-30-generic x86_64)
这是我的日志文件: 什么是开发工具问题?
[0.973][INFO]: COMMAND InitSession {
"desiredCapabilities": {
"browserName": "chrome",
"chromeOptions": {
"args": [ ],
"extensions": [ ]
},
"javascriptEnabled": true,
"platform": "ANY",
"version": ""
}
}
[0.980][INFO]: Populating Preferences file: {
"alternate_error_pages": {
"enabled": false
},
"autofill": {
"enabled": false
},
"browser": {
"check_default_browser": false
},
"distribution": {
"import_bookmarks": false,
"import_history": false,
"import_search_engine": false,
"make_chrome_default_for_user": false,
"show_welcome_page": false,
"skip_first_run_ui": true
},
"dns_prefetching": {
"enabled": false
},
"profile": {
"content_settings": {
"pattern_pairs": {
"https://*,*": {
"media-stream": {
"audio": "Default",
"video": "Default"
}
}
}
},
"default_content_settings": {
"geolocation": 1,
"mouselock": 1,
"notifications": 1,
"popups": 1,
"ppapi-broker": 1
},
"password_manager_enabled": false
},
"safebrowsing": {
"enabled": false
},
"search": {
"suggest_enabled": false
},
"translate": {
"enabled": false
}
}
[0.982][INFO]: Populating Local State file: {
"background_mode": {
"enabled": false
},
"ssl": {
"rev_checking": {
"enabled": false
}
}
}
[0.988][INFO]: Launching chrome: /usr/bin/chromium-browser --disable-background-networking --disable-client-side-phishing-detection --disable-component-update --disable-default-apps --disable-hang-monitor --disable-prompt-on-repost --disable-sync --disable-web-resources --enable-logging --ignore-certificate-errors --load-extension=/tmp/.com.google.Chrome.Wgxcag/internal --log-level=0 --metrics-recording-only --no-first-run --password-store=basic --remote-debugging-port=12948 --safebrowsing-disable-auto-update --safebrowsing-disable-download-protection --test-type=webdriver --use-mock-keychain --user-data-dir=/tmp/.com.google.Chrome.Fqyvg3 data:,
[0.991][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[0.999][WARNING]: PAC support disabled because there is no system implementation
[1.113][DEBUG]: DevTools request failed
[1.165][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.166][DEBUG]: DevTools request failed
[1.217][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.217][DEBUG]: DevTools request failed
[1.268][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.269][DEBUG]: DevTools request failed
[1.319][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.320][DEBUG]: DevTools request failed
[1.370][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.371][DEBUG]: DevTools request failed
[1.422][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.423][DEBUG]: DevTools request failed
[1.474][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.475][DEBUG]: DevTools request failed
[1.526][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.527][DEBUG]: DevTools request failed
[1.577][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.578][DEBUG]: DevTools request failed
[1:1:0123/165812:ERROR:image_metadata_extractor.cc(111)] Couldn't load libexif.
[1.629][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.630][DEBUG]: DevTools request failed
[6845:6845:0123/165812:ERROR:browser_main_loop.cc(209)] Gtk: cannot open display:
[1.680][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.681][DEBUG]: DevTools request failed
[1.731][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.735][DEBUG]: DevTools request failed
[1.786][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.787][DEBUG]: DevTools request failed
[1.837][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.838][DEBUG]: DevTools request failed
[1.889][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.891][DEBUG]: DevTools request failed
[1.942][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.944][DEBUG]: DevTools request failed
[1.995][DEBUG]: DevTools request: http://127.0.0.1:12948/json/version
[1.997][DEBUG]: DevTools request failed
您可以在虚拟显示器上使用 Xvfb
with the help of pyvirtualdisplay
到 运行 您的 chrome 自动化,因为您没有真实的显示器:
from pyvirtualdisplay import Display
from selenium import webdriver
display = Display(visible=0, size=(800, 600))
display.start()
browser = webdriver.Chrome()
browser.get('http://www.google.com')
print browser.title
browser.quit()
display.stop()
另请参阅:
- How do I run Selenium in Xvfb?
除此之外,切换到 PhantomJS
也是一种选择:
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get('http://www.google.com')
print browser.title
browser.quit()