python-selenum3 第三Apsara Infrastructure Management Framework本API——瀏覽器操作

來源:互聯網
上載者:User

標籤:切換   implicit   TE   html   查看   fir   等於   click   跳轉   

開啟網頁並驗證

#驗證網頁#開啟Firefoxdriver = webdriver.Firefox(executable_path = "d:\\geckodriver")#將地址賦值給變數fURLfURL = "https://www.baidu.com"#瀏覽driver.get(fURL)


瀏覽器向前向後重新整理

#開啟Firefoxdriver = webdriver.Firefox(executable_path = "d:\\geckodriver")#將地址賦值給變數fURL和sURLfURL = "http://www.baidu.com"sURL = "http://www.sogou.com"#get方法開啟百度頁面之後再開啟搜狗driver.get(fURL)driver.get(sURL)#後退到百度driver.back()#assert進行驗證 沒有發現這幾個字,出現後面的提示assert driver.title.find(u"百度一下") >= 0,"沒有出現"#前進到搜狗driver.forward()#assert進行驗證 沒有發現這幾個字,出現後面的提示assert driver.title.find(u"搜狗") >=0,"沒有出現"#重新整理當前頁面driver.refresh()

瀏覽器最大化

#開啟瀏覽器driver = webdriver.Firefox(executable_path = "d:\\geckodriver")#將網址賦值給fURL變數fURL = "http://www.baidu.com"#登入百度driver.get(fURL)#將視窗最大化driver.maximize_window()


擷取title屬性值,並確定 通常做證明網頁開啟正確性

#開啟瀏覽器driver = webdriver.Firefox(executable_path = "d:\\geckodriver")#將網址賦值給fURL變數fURL = "http://www.baidu.com"#進入百度首頁driver.get(fURL)#視窗最大化driver.maximize_window()#擷取title屬性並賦值給變數titletitle = driver.title#控制台輸出title資訊print("當前網頁的title屬性是:",title)#斷言編寫 下面的斷言是unittest架構的斷言方法 後期會用到 這裡只是為了屬性編寫#assertEqual(title,u"百度一下,你就知道","網頁title屬性值錯誤")

擷取HTML的原始碼,並斷言

driver = webdriver.Firefox(executable_path = "d:\\geckodriver")fURL = "http://www.baidu.com"driver.get(fURL)driver.maximize_window()#擷取網頁HTML原始碼 並賦值給pageSourcepageSource = driver.page_source#在控制台列印print(pageSource)#斷言編寫,查看原始碼中是否有貼吧這兩個字,一樣是unittest架構斷言#assertTrue(u"貼吧" in pageSource,"沒有找到貼吧這兩個字")

查看網站地址,並斷言

driver = webdriver.Firefox(executable_path="d:\\geckodriver")fURL = "http://www.baidu.com"driver.get(fURL)#擷取地址並賦值給pageUrlpageUrl = driver.current_url#在控制台列印print(pageUrl)#斷言編寫,unittest架構斷言#assertEqual(pageUrl,"http://www.baidu.com","當前網頁網址非預期結果")

視窗跳轉

driver = webdriver.Firefox(executable_path="d:\\geckodriver")fURL = "https://news.baidu.com/"driver.get(fURL)#隱式等待driver.implicitly_wait(10)#擷取當前視窗控制代碼 並賦值給handlehandle = driver.current_window_handle#驗證控制台列印print(handle)#定位網頁、貼吧等連結整體賦值給ss = driver.find_element_by_partial_link_text("網頁")#點擊第一個按鈕s.click()#顯示所有的控制代碼賦值給all_hall_h = driver.window_handles#迴圈判斷是否與首頁控制代碼相等for i in all_h:    if i != handle:        #如果不等於就切換        driver.switch_to.window(i)print(driver.title)#驗證if u"百度一下" in driver.title:    print(u"頁面開啟正常")else:    print(u"失敗")#返回driver.switch_to.window(handle)

python-selenum3 第三Apsara Infrastructure Management Framework本API——瀏覽器操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.