python - Phantomjs添加到环境变量后为什么webdriver.PhantomJS()不能用默认的path参数?

查看:327
本文介绍了python - Phantomjs添加到环境变量后为什么webdriver.PhantomJS()不能用默认的path参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问 题

已经将phantomjs添加到环境变量中,按照selenium的文档里说这样就可以用webdriver.PhantomJS()的executable_path的默认参数运行了,但实测会报错.

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.get(url)
print(driver.page_source)
driver.quit()

以下是报错内容:

Traceback (most recent call last):
File "D:Pythonlibsite-packagesseleniumwebdrivercommonservice.py", line 74, in start

stdout=self.log_file, stderr=self.log_file)

File "D:Pythonlibsubprocess.py", line 707, in init

restore_signals, start_new_session)

File "D:Pythonlibsubprocess.py", line 990, in _execute_child

startupinfo)

FileNotFoundError: [WinError 2] 系统找不到指定的文件。

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "F:/ProgrammingWorkspaces/Python/BbGrade/tests/selenium_test.py", line 3, in <module>

driver = webdriver.PhantomJS()

File "D:Pythonlibsite-packagesseleniumwebdriverphantomjswebdriver.py", line 52, in init

self.service.start()

File "D:Pythonlibsite-packagesseleniumwebdrivercommonservice.py", line 81, in start

os.path.basename(self.path), self.start_error_message)

selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH

另外还有个小问题是为什么cmd中用phantomjs每一句下面都会有个undefined

解决方案

try driver = webdriver.PhantomJS(executable_path="phantomjs.exe")

每一句下面都会有个undefined 是命令行带的,同浏览器的控制台 可忽略

这篇关于python - Phantomjs添加到环境变量后为什么webdriver.PhantomJS()不能用默认的path参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆