我正在嘗試通過 啟動 selenium 應用程式(使用 python)webdriver.Remote(),就像他們在檔案中推薦的那樣,但是當我運行下面的代碼時,我收到以下錯誤訊息:
selenium.common.exceptions.WebDriverException: Message: default backend - 404
我的代碼在這里:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.set_capability("platformName", "LINUX")
chrome_options.set_capability("browserName", "chrome")
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
prefs = {"download.default_directory": 'downloads'}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=chrome_options.to_capabilities()
)
driver.get("http://www.google.com")
search_box = driver.find_element_by_name('q')
search_box.send_keys('stackoverflow')
search_box.submit()
driver.quit()
我已經嘗試洗掉選項并僅使用推薦的代碼,如下所示,但我遇到了同樣的問題。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
command_executor='http://127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME
)
driver.get("http://www.google.com")
search_box = driver.find_element_by_name('q')
search_box.send_keys('stackoverflow')
search_box.submit()
driver.quit()
uj5u.com熱心網友回復:
我找到了問題的解決方案,它與command_executor. 每當你需要宣告這個引數時,不要把httpor放在https地址之前。所以正確的代碼是:
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.set_capability("platformName", "LINUX")
chrome_options.set_capability("browserName", "chrome")
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
prefs = {"download.default_directory": 'downloads'}
chrome_options.add_experimental_option("prefs", prefs)
driver = webdriver.Remote(
command_executor='127.0.0.1:4444/wd/hub',
desired_capabilities=chrome_options.to_capabilities()
)
driver.get("http://www.google.com")
search_box = driver.find_element_by_name('q')
search_box.send_keys('stackoverflow')
search_box.submit()
driver.quit()
或者更簡單的代碼:
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
driver = webdriver.Remote(
command_executor='127.0.0.1:4444/wd/hub',
desired_capabilities=DesiredCapabilities.CHROME
)
driver.get("http://www.google.com")
search_box = driver.find_element_by_name('q')
search_box.send_keys('stackoverflow')
search_box.submit()
driver.quit()
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/402969.html
標籤:
下一篇:如何在Rails中隱藏查詢字串
