[英]Running Selenium WebDriver python bindings in chrome
我在使用 Selenium 時遇到了問題。 對於我的項目,我必須使用 Chrome。 但是,使用 Selenium 啟動該瀏覽器后,我無法連接到該瀏覽器。
出於某種原因,Selenium 無法自己找到 Chrome。 當我嘗試在不包含路徑的情況下啟動 Chrome 時會發生這種情況:
Traceback (most recent call last):
File "./obp_pb_get_csv.py", line 73, in <module>
browser = webdriver.Chrome() # Get local session of chrome
File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
self.service.start()
File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 58, in start
and read up at http://code.google.com/p/selenium/wiki/ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'ChromeDriver executable needs to be available in the path. Please download from http://code.google.com/p/selenium/downloads/list and read up at http://code.google.com/p/selenium/wiki/ChromeDriver'
為了解決這個問題,我隨后在啟動 Chrome 的代碼中包含了 Chromium 路徑。 但是,解釋器無法找到要連接的套接字:
Traceback (most recent call last):
File "./obp_pb_get_csv.py", line 73, in <module>
browser = webdriver.Chrome('/usr/bin/chromium') # Get local session of chrome
File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/webdriver.py", line 46, in __init__
self.service.start()
File "/usr/lib64/python2.7/site-packages/selenium/webdriver/chrome/service.py", line 64, in start
raise WebDriverException("Can not connect to the ChromeDriver")
selenium.common.exceptions.WebDriverException: Message: 'Can not connect to the ChromeDriver'
我還嘗試通過啟動 chrome 來解決這個問題:
chromium --remote-shell-port=9222
然而,這也不起作用。
附注。 以下是有關我的系統的一些信息:
www-client: chromium 15.0.874.121 dev-lang: python 2.7.2-r3 Selenium 2.11.1 OS: GNU/Linux Gentoo Kernel 3.1.0-gentoo-r1
您需要確保獨立的 ChromeDriver 二進制文件(與 Chrome 瀏覽器二進制文件不同)在您的路徑中或在 webdriver.chrome.driver 環境變量中可用。
有關如何連接的完整信息,請參閱http://code.google.com/p/selenium/wiki/ChromeDriver 。
編輯:
是的,似乎是 Python 綁定中的一個錯誤,即從路徑或環境變量中讀取 chromedriver 二進制文件。 似乎如果 chromedriver 不在您的路徑中,您必須將其作為參數傳遞給構造函數。
import os
from selenium import webdriver
chromedriver = "/Users/adam/Downloads/chromedriver"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
driver.get("http://stackoverflow.com")
driver.quit()
對於 Linux
檢查您是否安裝了最新版本的 chrome brwoser-> chromium-browser -version
如果沒有,請安裝最新版本的 chrome sudo apt-get install chromium-browser
從這里獲取適當版本的 chrome 驅動程序
解壓 chromedriver.zip
將文件移動到/usr/bin
目錄sudo mv chromedriver /usr/bin
轉到/usr/bin
目錄cd /usr/bin
現在,您需要運行諸如sudo chmod a+x chromedriver
來將其標記為可執行。
最后你可以執行代碼。
from selenium import webdriver driver = webdriver.Chrome() driver.get("http://www.google.com") print driver.page_source.encode('utf-8') driver.quit()
僅限 Mac OSX
一個更簡單的開始方法(假設你已經安裝了自制軟件,如果沒有,你應該先這樣做,讓自制軟件讓你的生活更美好)是運行以下命令:
brew install chromedriver
這應該將 chromedriver 放在您的路徑中,並且您應該已准備就緒。
對於 Windows,請將chromedriver.exe
放在<Install Dir>/Python27/Scripts/
有兩種方法可以在 Google Chrome 中運行 Selenium python 測試。 我正在考慮 Windows(在我的情況下是 Windows 10):
先決條件:從以下位置下載最新的 Chrome 驅動程序: https : //sites.google.com/a/chromium.org/chromedriver/downloads
方式一:
i) 將下載的 zip 文件解壓縮到您選擇的目錄/位置
ii) 在您的代碼中設置可執行路徑,如下所示:
self.driver = webdriver.Chrome(executable_path='D:\Selenium_RiponAlWasim\Drivers\chromedriver_win32\chromedriver.exe')
方式二:
i) 只需將 chromedriver.exe 粘貼到 /Python/Scripts/ 下(在我的情況下,文件夾是:C:\\Python36\\Scripts)
ii) 現在編寫簡單的代碼如下:
self.driver = webdriver.Chrome()
對於 Windows 的 IDE:
如果您的路徑不起作用,您可以嘗試將chromedriver.exe
添加到您的項目中,就像在此項目結構中一樣。
然后你應該在你的主文件中加載chromedriver.exe
。 至於我,我在driver.exe
中加載了driver.py
。
def get_chrome_driver():
return webdriver.Chrome("..\\content\\engine\\chromedriver.exe",
chrome_options='--no-startup-window')
..
表示driver.py's
上層目錄
.
表示driver.py
所在的目錄
希望這會有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.