Python+Selenium--啟動及關閉瀏覽器


場景

頁面上彈出的對話框是自動化測試經常會遇到的一個問題;很多情況下對話框是一個iframe,如之前iframe介紹的例子,處理起來稍微有點麻煩;但現在很多前端框架的對話框是div 形式的,這就讓我們的處理變得十分簡單。

代碼

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#!/usr/bin/env python
# -*- codinfg:utf-8 -*-
'''
@author: Jeff LEE
@file: 對話框.py
@time: 2020-01-03 9:52
@desc:
'''
from  selenium  import  webdriver
#導入WebDriverWait
from  selenium.webdriver.support.ui  import  WebDriverWait
import  time
 
driver  =  webdriver.Firefox()
 
driver.get( 'https://www.baidu.com/' )
 
element = driver.find_elements_by_name( "tj_login" )
 
for  ele0  in  element:
     if  ele0.is_displayed():
         ele0.click()
#切換用戶名登錄方式
user_login  = WebDriverWait(driver, 30 ).until( lambda  driver:driver.find_element_by_id( 'TANGRAM__PSP_10__footerULoginBtn' ) )
user_login.click()
time.sleep( 2 )
 
#輸入用戶名
driver.find_element_by_id( 'TANGRAM__PSP_10__userName' ).send_keys( '123456' )
time.sleep( 2 )
 
#輸入密碼
driver.find_element_by_id( 'TANGRAM__PSP_10__password' ).send_keys( '123456' )
time.sleep( 2 )
 
#提交
driver.find_element_by_id( 'TANGRAM__PSP_10__submit' ).submit()
time.sleep( 2 )
 
driver.quit()

  

備注:

定位元素可以使用等待時間的方法,上面代碼可自行修改


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM