selenium使用chrome浏览器运行的几种方式

安装了selenium后,直接用Firefox浏览器即可运行,如果要使用别的浏览器chrome、ie等运行python脚本,需要下载相应的驱动文件:
下面以chrome为例,介绍几种形式运行chrome浏览器
(chromedriver下载地址:https://sites.google.com/a/chromium.org/chromedriver/

  • 方式一

  1. 下载chromedriver驱动,将该文件解压后,拷贝到路径/Users/xxxxxx/files/python/selfPractise/Test_framework/drivers下
    (python脚本所在路径是:/Users/xxxxxx/files/python/selfPractise/Test_framework/test/case/test_baidu_2.py)
  2. 启动chrome浏览器脚本:
driver = webdriver.Chrome(executable_path=DRIVER_PATH + '/chromedriver')  #其中executable_path等号右边的路径即是chromedriver的所在路径
driver.get(URL)
  • 方式二:

  1. 将chromedriver拷贝到/Users/xxxxxx/anaconda/bin/下面
  2. 启动chrome浏览器脚本:
_chrome_options = Options()
_chrome_options.add_argument('disable-infobars')
self.driver = webdriver.Chrome()
self.driver.get(self.URL)
  • 方式三:

和方式一类似,只是python脚本做了下优化

  1. 下载chromedriver驱动,将该文件解压后,拷贝到路径/Users/xxxxxx/files/python/selfPractise/Test_framework/drivers下
    (python脚本所在路径是:/Users/xxxxxx/files/python/selfPractise/Test_framework/test/case/test_baidu_2.py)
  2. 启动chrome浏览器脚本:
_chrome_options = Options()
_chrome_options.add_argument('disable-infobars')
driver = webdriver.Chrome(executable_path=DRIVER_PATH + '/chromedriver',chrome_options=_chrome_options)  #其中executable_path等号右边的路径即是chromedriver的所在路径
driver.get(URL)
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容