即使PATHed正常运行,Python Selenium Webdriver仍然无法运行,并且脚本按照指示编写

时间:2017-01-09 20:02:43

标签: java python selenium selenium-webdriver webdriver

我正在尝试学习Python的Webdriver,使用对Python的基本了解,以及对Selenium和JAVA的更广泛理解。我按照找到的here指南进行了操作。我的代码:

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys

driver = webdriver.Firefox()
driver.get("http://www.google.com")
assert "Google" in driver.title
sb = driver.find_element_by_name(lst-ib)
sb.clear()
sb.send_keys("Youtube")
sb.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()

现在,在PyCharm中运行它将返回:

C:\Users\mbrenn002c\AppData\Local\Programs\Python\Python35-32\python.exe C:/Users/mbrenn002c/PycharmProjects/PyDriver/Webdriver.py
Traceback (most recent call last):
  File "C:/Users/mbrenn002c/PycharmProjects/PyDriver/Webdriver.py", line 5, in <module>
    driver = webdriver.Firefox()
  File "C:\Users\mbrenn002c\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 145, in __init__
    keep_alive=True)
  File "C:\Users\mbrenn002c\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 92, in __init__
    self.start_session(desired_capabilities, browser_profile)
  File "C:\Users\mbrenn002c\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 179, in start_session
response = self.execute(Command.NEW_SESSION, capabilities)
  File "C:\Users\mbrenn002c\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
  File "C:\Users\mbrenn002c\AppData\Local\Programs\Python\Python35-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: Expected browser binary location, but unable to find binary in default location, no 'moz:firefoxOptions.binary' capability provided, and no binary flag set on the command line


Process finished with exit code 1

我的pip包是:selenium; beautifulsoup4。

我的PATH看起来像这样:

%USERPROFILE%\AppData\Local\Mirosoft\WindowsApps;C:\Users\myuser\AppData\Programs\Python;C:\Pythone34;C:\Users\myuser\Desktop\File transfer\Eclipse Items\geckodrver.exe

我的主要问题是;我究竟做错了什么?据我所知,我已经正确地遵循了一切,这段代码应该打开geckodriver并按照列出的方式工作。它甚至不能运行selenium独立服务器来运行webdriver。

我尝试在我的QPython客户端上使用相同的pip和代码运行它来安装我的Android手机,它在控制台中返回了一些回调,最后是这样:

 Exception AttributeError: "'Service' object has no attribute 'log_file'" in <bound method Service.__del__ of <seleniumwebdriver.firefox.service.Service object at 0xf5e709f0>> ignored

可能值得注意的是,我的手机还没有Root,我真正做的就是保存这个脚本,然后点击安装Selenium和beautifulsoup4。

1 个答案:

答案 0 :(得分:0)

  

selenium.common.exceptions.WebDriverException:消息:预期   浏览器二进制位置,但默认情况下无法找到二进制文件   location,没有提供'moz:firefoxOptions.binary'功能,没有   在命令行上设置二进制标志

异常显然表明Firefox已安装了与Selenium不同的目录。尝试访问默认路径但找不到。您需要描述代码中firefox的安装位置。

使用下面的代码段

import unittest
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary

binary = FirefoxBinary('path/to/installed firefox binary')
driver = webdriver.Firefox(firefox_binary=binary)

driver.get("http://www.google.com")
assert "Google" in driver.title
sb = driver.find_element_by_name(lst-ib)
sb.clear()
sb.send_keys("Youtube")
sb.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()