嗨我有一个覆盆子pi 3并决定尝试使用python 3运行selenium。我使用“pip3 install selenium”安装它并且没有错误。用我的代码制作了一个小测试脚本后:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# create a new Firefox session
driver = webdriver.Firefox()
driver.implicitly_wait(30)
driver.maximize_window()
# Navigate to the application home page
driver.get("http://www.google.com")
# get the search textbox
search_field = driver.find_element_by_id("lst-ib")
search_field.clear()
# enter search keyword and submit
search_field.send_keys("Selenium WebDriver Interview questions")
search_field.submit()
# get the list of elements which are displayed after the search
# currently on result page using find_elements_by_class_name method
lists= driver.find_elements_by_class_name("_Rm")
# get the number of elements found
print ("Found " + str(len(lists)) + " searches:")
# iterate through each element and print the text that is
# name of the search
i=0
for listitem in lists:
print (listitem.get_attribute("innerHTML"))
i=i+1
if(i>10):
break
# close the browser window
driver.quit()
但它给了我一个错误:
Traceback (most recent call last):
File "/home/pi/SeleniumTest.py", line 5, in <module>
driver = webdriver.Firefox()
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/firefox/webdriver.py", line 170, in __init__
keep_alive=True)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 156, in __init__
self.start_session(capabilities, browser_profile)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 245, in start_session
response = self.execute(Command.NEW_SESSION, parameters)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/webdriver.py", line 314, in execute
self.error_handler.check_response(response)
File "/home/pi/.local/lib/python3.5/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: connection refused
感谢任何帮助,谢谢:)
答案 0 :(得分:0)
您需要安装geckodriver,这是一项要求,我不知道apt是否可以使用,但如果没有,您可以从GitHub上的官方发布页面下载armv7hf版本{{3} }
如果您需要手动安装,请将可执行文件geckodriver
解压缩到路径中的目录,通常为/usr/local/bin
或&#39; $ HOME / bin`
编辑: 另一个可能的原因是通过SSH运行selenium,在这种情况下你的RPi没有显示器来连接firefox窗口并且它会崩溃,为了解决这个问题,你需要在无头模式下运行Firefox,如下所示:
# create a new Firefox session
opt = webdriver.FirefoxOptions()
opt.set_headless()
driver = webdriver.Firefox(options=opt)
# here instead of maximize you have to set a custom size
driver.set_window_size(1920, 1080)
答案 1 :(得分:0)
此错误消息......
selenium.common.exceptions.WebDriverException: Message: connection refused
...意味着 GeckoDriver 无法成功启动/生成新的 Web客户端会话/实例。
您正在使用的二进制文件的版本信息可能会让我们更深入地了解您正在观察的错误。
但是,你可能需要传递 Key executable_path 以及 Value ,引用 GeckoDriver的绝对路径作为参数初始化 WebDriver 和 WebBrowser ,如下所示:
from selenium import webdriver
# create a new Firefox session
driver = webdriver.Firefox(executable_path=r'/path/to/geckodriver')
Test
。driver.quit()
方法中调用tearDown(){}
以关闭&amp;正常销毁 WebDriver 和 Web客户端实例。