2017-01-09 66 views
0

我想学习Python的Webdriver,使用Python的基本理解,以及对Selenium和JAVA的更广泛的理解。我遵循指南发现here。我的代码:Python Selenium Webdriver不会运行,即使正确的PATH和脚本写作指示

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封装:硒; beautifulsoup4。

我的路径如下这样:

%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客户端上我的Android手机,这回一些回调在控制台运行它,这个到底:

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

它可能值得注意的是我的手机没有Rooted,而我所做的只是保存这一个脚本,并且安装了Selenium和beautifulsoup4。

+0

我没有从手机发布完整的回复日志,因为它的长度和可能与问题缺乏相关性。如果需要,我可以发布它,或者详细阐述/扩展任何问题的部分。 –

回答

0

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

异常很明显,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() 
+0

运行代码并为geckodriver补充正确的目录。它会输出以下内容:C:\ Users \ mbrenn002c \ AppData \ Local \ Programs \ Python \ Python35-32 \ python.exe C:/Users/mbrenn002c/PycharmProjects/PyDriver/Webdriver.py 文件“C:/ Users/mbrenn002c /PycharmProjects/PyDriver/Webdriver.py“,第6行 binary = FirefoxBinary('C:\ Users \ mbrenn002c \ Desktop \ File transfer \ C drive drops \ eclipse') ^ SyntaxError :(unicode error)'unicodeescape'codec无法解码位置2-3中的字节:截断\ UXXXXXXXX转义 过程完成退出代码1 –

+0

'C:\ Users \ mbrenn002c \ Desktop \ File transfer \ C驱动器滴\ eclipse'空格“C驱动器丢弃”是语法问题的原因。请改正,就像“C_drive_drops” – nuriselcuk

+0

我已更正空格和'_'在我的代码中的所有组合,它仍会引发相同的错误。 –

相关问题