在我的IntellIJ上安装了Selenium。我正在努力完成练习2.1 在http://selenium-python.readthedocs.io/getting-started.html。
这是我的代码:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome("C:\Python27\Scripts/")
driver.get("http://www.python.org")
assert "Python" in driver.title
elem = driver.find_element_by_class_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
assert "No results found." not in driver.page_source
driver.close()
产生以下错误:
Traceback (most recent call last):
File "B:\Program Files (x86)\lib\site-packages\selenium\webdriver\common\service.py", line 74, in start
stdout=self.log_file, stderr=self.log_file)
File "B:\Program Files (x86)\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "B:\Program Files (x86)\lib\subprocess.py", line 990, in _execute_child
startupinfo)
PermissionError: [WinError 5] Access is denied
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "B:/Alexander/Documents/PythonPrograms/Selenium/Test.py", line 3, in <module>
driver = webdriver.Chrome("C:\Python27\Scripts/")
File "B:\Program Files (x86)\lib\site-packages\selenium\webdriver\chrome\webdriver.py", line 62, in __init__
self.service.start()
File "B:\Program Files (x86)\lib\site-packages\selenium\webdriver\common\service.py", line 86, in start
os.path.basename(self.path), self.start_error_message)
**selenium.common.exceptions.WebDriverException: Message: '' executable may have wrong permissions**. Please see https://sites.google.com/a/chromium.org/chromedriver/home
答案 0 :(得分:0)
The code that triggers the exception看起来像这样:
def start(self):
<...>
try:
cmd = [self.path]
cmd.extend(self.command_line_args())
self.process = subprocess.Popen(cmd, <...>)
<...>
except OSError as err:
<...>
elif err.errno == errno.EACCES:
raise WebDriverException(
"'%s' executable may have wrong permissions. %s" % (
os.path.basename(self.path), self.start_error_message)
如您所见,错误消息中的''
表示os.path.basename(self.path)
为空。并且这样表达的信息表明代码的作者并不打算将其视为空。
让我们看看它的初始化内容。 Searching the repo for references将我们带到py/selenium/webdriver/common/service.py:37
:
class Service:
def __init__(self, executable, port=0, log_file=DEVNULL, env=None, start_error_message=""):
self.path = executable
<...>
这意味着,self.path
是您传递给webdriver.Chrome
的第一个参数,即"C:\Python27\Scripts/"
。因为它作为要执行的程序传递给subprocess
,它应该是可执行文件的路径,而不是目录。根据上述搜索中的其他结果判断, Chrome可执行文件的路径。在该示例中,Firefox驱动程序在没有参数的情况下启动,但正如搜索结果所示,它似乎从配置文件或其他东西进行自动检测。