我第一次尝试使用selenium webdriver。我已经更新到Python 3.6,我也重新安装了selenium。试图打开基本网页已经出错了。这是代码:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
这是非常基本但它仍然无法正常工作。它抛出了一些超出我口译技巧的错误。当然,我试过谷歌搜索问题似乎没有任何帮助。我很感激任何意见。这些是错误:
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "C:\Python36\lib\subprocess.py", line 707, in __init__restore_signals, start_new_session)
File "C:\Python36\lib\subprocess.py", line 990, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] The system cannot find the file specified
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:/Users/Will Pickard/PycharmProjects/Basics/Webdriver.py", line 3, in <module>
driver = webdriver.Firefox()
File "C:\Python36\lib\site-packages\selenium\webdriver\firefox\webdriver.py", line 140, in __init__
self.service.start()
File "C:\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 81, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable needs to be in PATH.
Exception ignored in: <bound method Service.__del__ of <selenium.webdriver.firefox.service.Service object at 0x03801170>>
Traceback (most recent call last):
File "C:\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 173, in __del__
self.stop()
File "C:\Python36\lib\site-packages\selenium\webdriver\common\service.py", line 145, in stop
if self.process is None:
AttributeError: 'Service' object has no attribute 'process'
答案 0 :(得分:1)
从几个版本开始,Selenium停止为Firefox提供本机支持,现在依赖于使用外部浏览器驱动程序进行控制。下载可用的gecko webdriver并使用以下代码:
from selenium import webdriver
ff = "/path/to/geckodriver"
driver = webdriver.Firefox(executable_path=ff)
答案 1 :(得分:0)
您必须安装geckodriver(适用于v47之后的firefox等gecko浏览器)或chromedriver(适用于Chrome浏览器)。 安装后,您应该能够使用下面提到的配置执行代码。
您可以将DesiredCapabilities设置为 FIREFOX 并指向驱动程序二进制文件。您应该能够使用这些功能配置驱动程序并检索所需的页面。
from selenium import webdriver
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
firefox_capabilities['binary'] = '/usr/local/bin/geckodriver'
driver = webdriver.Firefox(capabilities=firefox_capabilities)
driver.get("http://www.python.org")
或者,如果您不确定是否使用了较新版本的Firefox,那么您可以在不设置DesiredCapabilities的情况下执行此类操作:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
driver = webdriver.Firefox(firefox_binary=FirefoxBinary('/usr/local/bin/geckodriver'))
driver.get("http://www.python.org")