Windows ChromeDriver+Python環(huán)境搭建

Windows webdriver環(huán)境搭建 (Chrome)

瀏覽器:Chrome 版本 64.0.3282.186(正式版本) (32 位)
ChromeDriver:當前用的版本 2.32.498550 下載地址
selenium:selenium-3.11.0-py2.py3-none-any.whl

操作步驟:

  • 1、將ChromeDriver放在d:\python27目錄下
  • 2、下載selenium-3.11.0-py2.py3-none-any.whl 并通過pip install file 安裝到python環(huán)境
  • 3、驗證selenium是否安裝成功:
    打開python ide ,輸入import selenium , 沒有報錯,成功!
  • 4、創(chuàng)建一個腳本,測試是否能夠自動啟動Chrome瀏覽器,自動進行操作

遇到的問題:

1、 創(chuàng)建文件名稱為selenium.py,導致引入庫失敗,原因:

原因: 在當前目錄有名叫selenium.py的文件,Python會先導入這個文件,然后再導入標準庫里面的selenium.py。
解決辦法: 可以使用

import selenium 
print selenium.__file__

去看打印出的文件路徑,如果不是類型下面的地址
C:\Python27\lib\site-packages\selenium-2.31.0-py2.7.egg\selenium__init__.pyc
需要把當前目錄下的名叫selenium文件刪除或者重命名

2、無法掉起Chrome

原因:沒有將ChromeDriver.exe放到 Python27目錄下,導致無法掉起Chrome
解決辦法: 將ChromeDriver.exe放到Python27目錄下就可以了

3、 運行錯誤:

RuntimeWarning: Parent module 'requests' not found while handling absolute import import selenium

報錯信息

RuntimeWarning: Parent module '...' not found while handling absolute import
import selenium
RuntimeWarning: Parent module '...' not found while handling absolute import
import time
RuntimeWarning: Parent module '...' not found while handling absolute import
import unittest   

原因及解決辦法:

這是一個PyCharmd的已知問題,從2016.2版本就存在,目前為止還沒解決。BUG地址
目前的解決辦法是使用舊的utrunner.py替換新的utrunner.py

1、先找到pycharm目錄
D:\Program Files (x86)\JetBrains\PyCharm 2016.3.2\helpers\pycharm

2、備份:utrunner.py
3、用舊的utrunner.py替換pycharm目錄下的utrunner.py

附上舊版本: utrunner.py

測試環(huán)境

訪問百度并進行搜索

# coding:utf-8

import selenium
print selenium.__file__
from selenium import webdriver
from selenium.webdriver.support.ui import Select
import unittest
import time


class Baidu(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.baidu.com"
        self.verificationErrors = []
        self.accept_next_alert = True #是否繼續(xù)接受下一個警告

    def test_baidu_serach(self):
        u"""百度用例搜索"""
        driver = self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_id("kw").send_keys("Selenium webdriver")
        driver.find_element_by_id("su").click()
        time.sleep(2)
        driver.close()

    def test_baidu_set(self):
        driver = self.driver
        driver.get(self.base_url+"/gaoji/preferences.html") #進入搜索設(shè)置頁

        Select(driver.find_element_by_id("nr")).select_by_visible_text(u"每頁顯示10條")
        time.sleep(2)
        driver.find_element_by_id("save").click()
        time.sleep(2)
        #driver.switch_to_alert().accept()
        driver.switch_to.alert.accept()
        time.sleep(2)
        #保存百度的設(shè)置時會會彈出一個確定按鈕;我們并沒按照常規(guī)的方法去定位彈窗上的“確定”按鈕,而是使用
        #driver.switch_to_alert().accept()

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([],self.verificationErrors)


if __name__ == "__main__":
    # unittest.main()
    suite = unittest.TestSuite()
    suite.addTest(Baidu("test_baidu_serach"))
    suite.addTest(Baidu("test_baidu_set"))
    unittest.TextTestRunner().run(suite)

運行成功!

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容