selenium自动化打开浏览器网页

注:Python版本3.6

打开浏览器,百度页面

coding=utf-8

from selenium import webdriver
driver=webdriver.Chrome() #调用chrome浏览器
driver.get('https://www.baidu.com')
print (driver.title)

driver.quit()

不同浏览器所需插件不同,按手头的资源情况,选择合适的测试环境。

Chrome

driver=webdriver.Chrome() #调用chrome浏览器

IE

driver=webdriver.Ie()
报错: 

os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'IEDriverServer.exe' exe
cutable needs to be in PATH. Please download from http://selenium-release.storag
e.googleapis.com/index.html and read up at https://github.com/SeleniumHQ/seleniu
m/wiki/InternetExplorerDriver

需要下载:IEDriverServer.exe
IE还需要其它的安全配置,具体需要时自行百度

FireFox
问题同IE:

Traceback (most recent call last):
  File "C:UsersAdministratorDesktoppython.py", line 4, in <module>
    driver=webdriver.Firefox()
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython36libsite-p
ackagesseleniumwebdriverfirefoxwebdriver.py", line 142, in init
    self.service.start()
  File "C:UsersAdministratorAppDataLocalProgramsPythonPython36libsite-p
ackagesseleniumwebdrivercommonservice.py", line 81, in start
    os.path.basename(self.path), self.start_error_message)
selenium.common.exceptions.WebDriverException: Message: 'geckodriver' executable
 needs to be in PATH.

需要下载:geckodriver

版权声明

弈心博客


本文首发site_name,转载请附上博文链接!