如何去掉phantomjs的驱动黑框

方法/步骤

  1. 打开命令行窗口,使用pip安装selenium包

     

     

    如何去掉phantomjs的驱动黑框_第1张图片

  2. 下载windows版的phantomjs.exe,放到c盘根目录

     

     

    如何去掉phantomjs的驱动黑框_第2张图片

  3. 打开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()

     

     

    如何去掉phantomjs的驱动黑框_第3张图片

  4. F5运行代码,selenium现在已经不再支持更新phantomjs,在shell中会提示出来,同时这样运行会弹出一个黑框就是phantomjs驱动的黑框

     

     

    如何去掉phantomjs的驱动黑框_第4张图片

  5. 要去掉黑框,打开python安装路径的C:\Python37\Lib\site-packages\selenium\webdriver\commones文件夹下的sevice.py文件,找到下面内容

     

     

    如何去掉phantomjs的驱动黑框_第5张图片

  6. 修改上一步内容如下:

     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)

     

    如何去掉phantomjs的驱动黑框_第6张图片

  7. 7

    再次运行代码,就不会弹出黑框了

     

     

    如何去掉phantomjs的驱动黑框_第7张图片

你可能感兴趣的:(Python杂记)