2017-01-09 65 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

我已更正空格和'_'在我的代碼中的所有組合,它仍會引發相同的錯誤。 –

相關問題