[英]How to run Selenium ChromeDriver from python3 on wsl2?
[英]how to run chromedriver on aws python
我正在使用硒進行刮擦,以下代碼在本地使用本地路徑工作。 我收到WebDriverException:消息:未知錯誤:找不到Chrome二進制文件。 我不確定是否收到此錯誤,因為我的路徑錯誤或必須安裝某些東西? 我在Google上待了好幾個小時,試圖弄清楚這一點,但是沒有運氣。
chrome_path = "/home/ec2-user/chrome/chromedriver"
driver = webdriver.Chrome(chrome_path)
book_categories_list = []
for i in asin:
try:
url = u"https://www.amazon.com/s/ref=nb_sb_noss?url=search-alias%3Dstripbooks&field-keywords="+str(i)
driver.get(url)
#time.sleep(1)
books = driver.find_element_by_class_name('a-expander-container')
book_categories_list.append(books.text)
except:
book_categories_list.append('Unknown')
---------------------------------------------------------------------------
WebDriverException Traceback (most recent call last)
<ipython-input-6-436f3e3e0dac> in <module>()
1 # Set driver and chrome driver path
2 chrome_path = "/home/ec2-user/chrome/chromedriver"
----> 3 driver = webdriver.Chrome(chrome_path)
4
5 # Url that I will access to scrape
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, options, service_args, desired_capabilities, service_log_path, chrome_options)
73 command_executor=ChromeRemoteConnection(
74 remote_server_addr=self.service.service_url),
---> 75 desired_capabilities=desired_capabilities)
76 except Exception:
77 self.quit()
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in __init__(self, command_executor, desired_capabilities, browser_profile, proxy, keep_alive, file_detector, options)
152 warnings.warn("Please use FirefoxOptions to set browser profile",
153 DeprecationWarning)
--> 154 self.start_session(desired_capabilities, browser_profile)
155 self._switch_to = SwitchTo(self)
156 self._mobile = Mobile(self)
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in start_session(self, capabilities, browser_profile)
241 parameters = {"capabilities": w3c_caps,
242 "desiredCapabilities": capabilities}
--> 243 response = self.execute(Command.NEW_SESSION, parameters)
244 if 'sessionId' not in response:
245 response = response['value']
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py in execute(self, driver_command, params)
310 response = self.command_executor.execute(driver_command, params)
311 if response:
--> 312 self.error_handler.check_response(response)
313 response['value'] = self._unwrap_value(
314 response.get('value', None))
~/anaconda3/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py in check_response(self, response)
240 alert_text = value['alert'].get('text')
241 raise exception_class(message, screen, stacktrace, alert_text)
--> 242 raise exception_class(message, screen, stacktrace)
243
244 def _value_or_default(self, obj, key, default):
WebDriverException: Message: unknown error: cannot find Chrome binary
(Driver info: chromedriver=2.37.544315 (730aa6a5fdba159ac9f4c1e8cbc59bf1b5ce12b7),platform=Linux 4.9.81-35.56.amzn1.x86_64 x86_64)
該錯誤確實給了我們如下提示:
WebDriverException: Message: unknown error: cannot find Chrome binary
該錯誤實質上意味着ChromeDriver在預期的位置找不到Chrome二進制文件。
可以有以下兩種解決方案:
根據要求 , Chrome的安裝位置必須確定。
或者,您可以通過Options
類的實例傳遞Chrome二進制文件的絕對路徑 ,如下所示:
from selenium.webdriver.chrome.options import Options options = Options() options.binary_location = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome"
最后,同時啟動的webdriver和Web客戶端 ,你需要與價值 chrome_path一起發送的說法主要 executable_path。
from selenium.webdriver.chrome.options import Options options = Options() options.binary_location = "/Applications/Google\\ Chrome.app/Contents/MacOS/Google\\ Chrome" chrome_path = "/home/ec2-user/chrome/chromedriver" driver = webdriver.Chrome(executable_path=chrome_path, chrome_options=options)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.