PhantomJS(已安装,将bin文件的路径添加到系统变量):
代码如下
from selenium import webdriver
browser = webdriver.PhantomJS()
browser.get('https://www.baidu.com')
print(browser.current_url)
报错如下:
E:\project\venv\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
Traceback (most recent call last):
File "E:\project\venv\lib\site-packages\selenium\webdriver\common\service.py", line 76, in start
stdin=PIPE)
File "E:\Anaconda3\lib\subprocess.py", line 709, in __init__
restore_signals, start_new_session)
File "E:\Anaconda3\lib\subprocess.py", line 997, in _execute_child
startupinfo)
FileNotFoundError: [WinError 2] 系统找不到指定的文件。
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "E:/project/allow/cuiqingcai_pdf/022_pyspider.py", line 2, in
browser = webdriver.PhantomJS()
File "E:\project\venv\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py", line 56, in __init__
self.service.start()
File "E:\project\venv\lib\site-packages\selenium\webdriver\common\service.py", line 83, in start
os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'phantomjs' executable needs to be in PATH.
在网上搜索了一下,加上自己的理解得到如下方案:
执行webdriver.PhantomJS()函数的时候,将PhantomJS的可执行文件的绝对路径作为参数传递给executable_path
代码修改后:
from selenium import webdriver
browser = webdriver.PhantomJS(executable_path=r"D:\phantomjs-2.1.1-windows\bin\phantomjs.exe")
browser.get('https://www.baidu.com')
print(browser.current_url)
执行结果能打印希望出现的"https://www.baidu.com/",但是还有警告:
E:\project\venv\lib\site-packages\selenium\webdriver\phantomjs\webdriver.py:49: UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
https://www.baidu.com/
翻译:
UserWarning: Selenium support for PhantomJS has been deprecated, please use headless versions of Chrome or Firefox instead
warnings.warn('Selenium support for PhantomJS has been deprecated, please use headless '
用户警告:对PhantomJS的Selenium支持已经被禁止,请使用Chrome或Firefox的无头版本
警告(“PhantomJS的Selenium支持已被拒绝,请使用head.”)
原因:
火狐浏览器和谷歌浏览器均在2017年6月前后在各自的软件里新增面向开发者进行自动化测试的无界面模式。
所谓无界面模式即借助该功能可以在操作系统里不出现可视化的GUI界面但同时保持浏览器在后台静默运行。
对于开发者来说通过该模式可以用来运行自动化测试,以便记录页面加载及按钮点击和表单填充的运行状况。
通过这些自动化测试记录可以帮助开发者改进网页代码,这样可优化网页的响应和加载并提高用户体验等等。
方法一:
使用无头浏览器,这里我用的浏览器是谷歌,火狐的话类似(但没试过)
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def main():
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(executable_path='./chromedriver', chrome_options=chrome_options)
driver.get("https://www.baidu.com")
print(driver.current_url)
driver.close()
if __name__ == '__main__':
main()
火狐的话类似(但没试过)
方法二:
降低chrome的版本,使其版本可适应其功能
参考:
https://blog.csdn.net/qq_30242609/article/details/79323963
https://blog.csdn.net/u010358168/article/details/79749149
https://blog.csdn.net/johinieli/article/details/76622776