方法/步骤
-
打开命令行窗口,使用pip安装selenium包
-
下载windows版的phantomjs.exe,放到c盘根目录
-
打开python开发工具IDLE,新建‘phtom.py’文件,并写代码如下:
from selenium import webdriver
import time
url='www.baidu.com'
driver = webdriver.PhantomJS(executable_path='c:\\phantomjs.exe')
driver.get(url)
time.sleep(20)
print (driver.page_source)
driver.quit() -
F5运行代码,selenium现在已经不再支持更新phantomjs,在shell中会提示出来,同时这样运行会弹出一个黑框就是phantomjs驱动的黑框
-
要去掉黑框,打开python安装路径的C:\Python37\Lib\site-packages\selenium\webdriver\commones文件夹下的sevice.py文件,找到下面内容
-
修改上一步内容如下:
self.process = subprocess.Popen(cmd, env=self.env,
close_fds=platform.system() != 'Windows',
stdout=self.log_file,
stderr=self.log_file,
stdin=PIPE,creationflags=134217728) -
7
再次运行代码,就不会弹出黑框了