銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�

1. 绠�浠�

銆�銆�鍓嶉潰鏂囩珷鎴戜滑浜嗚В浜嗗浣曡幏鍙栧厓绱犵殑text灞炴�у�硷紝鍜屽垽鏂厓绱犳槸鍚︽樉绀哄湪椤甸潰锛坕s_displayed()鏂规硶锛夛紝鏈枃鎴戜滑鏉ュ涔犱笅锛屽垽鏂竴涓帶浠舵槸鍚﹁閫変腑鐘舵�併�佽幏鍙栭〉闈㈠厓绱犵殑澶у皬銆佺粍鍚堥敭-鍏ㄩ�夋枃瀛椼�佺粍鍚堥敭-閫�鏍奸敭鍒犻櫎鏂囨湰鍜岄紶鏍囧彸閿瓑缁冧範鐨勫唴瀹广��

2. 楠岃瘉鎺т欢鏄惁琚�変腑

杩樻槸浠ョ櫨搴﹂椤电偣鍑荤櫥褰曞悗锛岃繘鍏ョ櫥褰曠晫闈㈢殑“涓嬫鑷姩鐧诲綍”涓句緥锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第1张图片

鐢变簬杩欎釜"涓嬫鑷姩鐧诲綍"鏄粯璁よ閫変腑鐘舵�侊紝鎴戜滑闇�瑕佺敤鑴氭湰鍘诲垽鏂槸鍚﹁閫変腑銆�

鐩稿叧浠g爜濡備笅锛�

2.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第2张图片

2.2 鍙傝�冧唬鐮侊細

# coding=utf-8馃敟

# 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛�

# 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆�
'''
Created on 2019-12-05
@author: 鍖椾含-瀹忓摜   QQ浜ゆ祦缇わ細705269076
Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒6
'''

# 3.瀵煎叆妯″潡
import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.baidu.com')
driver.implicitly_wait(8)

driver.find_element_by_xpath("//*[@id='u1']/a[7]").click()
time.sleep(1)
driver.find_element_by_xpath(".//*[@id='TANGRAM__PSP_10__footerULoginBtn']").click()
time.sleep(1)
try:
    driver.find_element_by_xpath(".//*[@id='TANGRAM__PSP_10__memberPass']").is_selected()
    print("Test Pass.")
except Exception as e:
    print('Test fail', format(e))

2.3 杩愯缁撴灉锛�

杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第3张图片

鎬荤粨锛�

鍏冪礌鏂规硶is_selected()杩斿洖鏄槸甯冨皵鍊硷紝鐢ㄦ潵鍒ゆ柇鍗曢�夋垨鑰呭閫夋帶浠舵槸鍚﹁閫変腑锛屾垨鑰呬笅鎷夐�夋嫨鑿滃崟鏄惁閫夋嫨涓�涓粯璁ょ殑option锛岄兘鍙互閫氳繃杩欎釜鏂规硶鍘诲垽鏂��

3. 鑾峰彇椤甸潰鍏冪礌澶у皬

鏈枃浠嬬粛濡備綍閫氳繃Selenium鏂规硶鍘昏幏鍙栭〉闈㈠厓绱犵殑澶у皬銆傝繖涓満鏅槸鍙兘鍦ㄩ渶瑕佹祴璇曠殑锛屼緥濡傚湪鍓嶇娴嬭瘯杩囩▼涓紝闇�瑕佸幓娴嬭瘯鏌愪竴涓帶浠舵槸鍚﹀拰璁捐澶у皬淇濇寔涓�鑷淬��

杩欓噷鐢ㄧ櫨搴﹂椤电殑“鐧惧害涓�涓�”杩欎釜鎸夐挳涓句緥锛屾墦鍗板嚭杩欎釜鎸夐挳鐨勫ぇ灏忋��

鐩稿叧鑴氭湰浠g爜濡備笅锛�

3.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第4张图片

3.2 鍙傝�冧唬鐮侊細

# coding=utf-8馃敟

# 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛�

# 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆�
'''
Created on 2019-12-05
@author: 鍖椾含-瀹忓摜   QQ浜ゆ祦缇わ細705269076
Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒5
'''

# 3.瀵煎叆妯″潡
import time
from selenium import webdriver

driver = webdriver.Chrome()
driver.maximize_window()
driver.implicitly_wait(6)
driver.get("https://www.baidu.com/")
time.sleep(1)
search_btn = driver.find_element_by_id('su')
print (search_btn.size)

3.3 杩愯缁撴灉锛�

杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第5张图片

4. 缁勫悎閿�-鍏ㄩ�夋枃瀛�

鏈枃寮�濮嬩粙缁峉elenium涓璌eys妯″潡涓嬬殑涓�浜涚粍鍚堬紝鎴栧揩鎹烽敭鐨勪娇鐢ㄣ�傚墠闈㈢殑涓�绡囨枃绔狅紝浠嬬粛浜嗗浣曟柊寮�涓�涓猼ab锛屼篃鏄敤鍒拌繖鍧楃殑鐭ヨ瘑銆�

鏈枃浠嬬粛鍏ㄩ�夊綋鍓嶉〉闈㈡枃瀛楋紝蹇嵎閿槸 Ctrl+a锛屼富瑕佽繕鏄敤send_keys()鏂规硶杈撳叆缁勫悎閿紝杩欓噷鐢ㄧ櫨搴﹂椤典妇渚嬨��

鐩稿叧鑴氭湰浠g爜濡備笅锛�

4.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第6张图片

4.2 鍙傝�冧唬鐮侊細

# coding=utf-8馃敟

# 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛�

# 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆�
'''
Created on 2019-12-05
@author: 鍖椾含-瀹忓摜   QQ浜ゆ祦缇わ細705269076
Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒5
'''

# 3.瀵煎叆妯″潡
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.baidu.com')

element = driver.find_element_by_tag_name('body')
element.send_keys(Keys.CONTROL + 'a')

4.3 杩愯缁撴灉锛�

杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第7张图片

 4.4 鐧惧害棣栭〉濡備笅锛�

鐧惧害棣栭〉鐩稿綋浜庢垜浠寜浜嗛敭鐩樼殑缁勫悎閿紙Ctrl + A锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第8张图片

5. 缁勫悎閿�-閫�鏍奸敭鍒犻櫎鏂囧瓧

鍓嶉潰涓�绡囨枃绔犱粙缁嶄簡锛屽父瑙勬竻闄ゆ枃鏈緭鍏ユ瀛楃鐨勬柟娉昪lear(), 鏈枃浠嬬粛

鐩稿叧鑴氭湰浠g爜濡備笅锛岃繖閲岀敤鐧惧害棣栭〉鎼滅储杈撳叆妗嗕妇渚嬶細

5.1 浠g爜瀹炵幇锛�

 銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第9张图片

5.2 鍙傝�冧唬鐮侊細

 

# coding=utf-8馃敟

# 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛�

# 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆�
'''
Created on 2019-12-05
@author: 鍖椾含-瀹忓摜   QQ浜ゆ祦缇わ細705269076
Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒5
'''

# 3.瀵煎叆妯″潡
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.baidu.com')

element = driver.find_element_by_id('kw')
element.send_keys("Selenium automation")
time.sleep(1)
element.send_keys(Keys.CONTROL+'a')
element.send_keys(Keys.BACKSPACE)

5.3 杩愯缁撴灉锛�

杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第10张图片

6. 榧犳爣鍙抽敭

鏈枃浠嬬粛濡備綍浣跨敤Selenium涓彸閿柟娉曟潵鎿嶄綔缃戦〉鍏冪礌銆傜綉椤典笂鏈変簺鍏冪礌鏄敮鎸佸彸閿潵瑙﹀彂鏂扮殑鑿滃崟锛岃繖閲屾壘涓�涓櫨搴﹂椤碉紝鐧惧害logo鐨勫彸閿紝鏌ョ湅鍥剧墖鍔ㄤ綔鏉ユā鎷熻繖涓満鏅��

闂鎷嗗垎锛�

1. 鎵撳紑鐧惧害棣栭〉锛屾壘鍒發ogo锛屽彸閿紶鏍�

2. 绉诲姩鍒拌彍鍗曪紝鏌ョ湅鍥惧儚锛岀劧鍚庣偣鍑�

鏍稿績闂锛屽浣曞彸閿搷浣滐紝鐒跺悗鏄浣曢�夋嫨鏌ョ湅鍥惧儚杩欎釜鑿滃崟銆傚湪Selenium涓紝鏈変竴涓狝ctionChains妯″潡鏀寔锛屽彸閿紝榧犳爣鎮仠锛屾嫋鎷斤紝鍙屽嚮绛夊姩浣溿�傛垜浠彲浠ラ�氳繃閿洏鍚戜笅绠ご鏉ラ�夋嫨鏌ョ湅鍥惧儚杩欎釜鑿滃崟锛岀劧鍚庣偣鍑诲氨鍙互杈惧埌鐩殑銆�

鐩稿叧鑴氭湰浠g爜濡備笅锛�

6.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第11张图片

6.2 鍙傝�冧唬鐮侊細

# coding=utf-8馃敟

# 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛�

# 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆�
'''
Created on 2019-12-05
@author: 鍖椾含-瀹忓摜   QQ浜ゆ祦缇わ細705269076
Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒5
'''

# 3.瀵煎叆妯″潡
import time
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys


driver = webdriver.Chrome()
driver.maximize_window()
driver.get('https://www.baidu.com')
time.sleep(2)

element = driver.find_element_by_xpath("//*[@id='lg']/img")
actionChains = ActionChains(driver)
actionChains.context_click(element).send_keys(Keys.ARROW_DOWN).send_keys(Keys.ENTER).perform()
#actionChains.context_click(element).send_keys('i').perform()

6.3 杩愯缁撴灉锛�

杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第12张图片

娉ㄦ剰锛�
ActionChains涓嬬浉鍏虫柟娉曞湪褰撳墠鐨刦irefox涓嶅伐浣滐紝杩欎釜鏄竴涓凡鐭ョ殑bug銆�

7. 灏忕粨

濂戒簡锛屼粖澶╃殑缁冧範灏卞埌杩欓噷锛屽笇鏈涘ぇ瀹跺ソ濂界殑缁冧範鍜岀悊瑙c�� 

 

鎮ㄧ殑鑲畾灏辨槸鎴戣繘姝ョ殑鍔ㄥ姏銆�濡傛灉浣犳劅瑙夎繕涓嶉敊锛屽氨璇烽紦鍔变竴涓嬪惂锛佽寰楃偣娉� 鎺ㄨ崘 涓嶈蹇樿鍝︼紒锛侊紒

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�_第13张图片

你可能感兴趣的:(銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�6-python+ selenium鑷姩鍖栨祴璇曪紙璇︾粏鏁欑▼锛�)