1. 程式人生 > >selenium web 自動化開發 —— 開啟瀏覽器

selenium web 自動化開發 —— 開啟瀏覽器

注: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:\Users\Administrator\Desktop\python.py", line 4, in <module>
    driver=webdriver.Firefox()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-p
ackages\selenium\webdriver\firefox\webdriver.py", line 142, in __init__
    self.service.start()
  File "C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-p
ackages\selenium\webdriver\common\service.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