1. 绠�浠�
銆�銆�鍓嶉潰鏂囩珷鎴戜滑浜嗚В浜嗗浣曡幏鍙栧厓绱犵殑text灞炴�у�硷紝鍜屽垽鏂厓绱犳槸鍚︽樉绀哄湪椤甸潰锛坕s_displayed()鏂规硶锛夛紝鏈枃鎴戜滑鏉ュ涔犱笅锛屽垽鏂竴涓帶浠舵槸鍚﹁閫変腑鐘舵�併�佽幏鍙栭〉闈㈠厓绱犵殑澶у皬銆佺粍鍚堥敭-鍏ㄩ�夋枃瀛椼�佺粍鍚堥敭-閫�鏍奸敭鍒犻櫎鏂囨湰鍜岄紶鏍囧彸閿瓑缁冧範鐨勫唴瀹广��
2. 楠岃瘉鎺т欢鏄惁琚�変腑
杩樻槸浠ョ櫨搴﹂椤电偣鍑荤櫥褰曞悗锛岃繘鍏ョ櫥褰曠晫闈㈢殑“涓嬫鑷姩鐧诲綍”涓句緥锛�
鐢变簬杩欎釜"涓嬫鑷姩鐧诲綍"鏄粯璁よ閫変腑鐘舵�侊紝鎴戜滑闇�瑕佺敤鑴氭湰鍘诲垽鏂槸鍚﹁閫変腑銆�
鐩稿叧浠g爜濡備笅锛�
2.1 浠g爜瀹炵幇锛�
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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
鎬荤粨锛�
鍏冪礌鏂规硶is_selected()杩斿洖鏄槸甯冨皵鍊硷紝鐢ㄦ潵鍒ゆ柇鍗曢�夋垨鑰呭閫夋帶浠舵槸鍚﹁閫変腑锛屾垨鑰呬笅鎷夐�夋嫨鑿滃崟鏄惁閫夋嫨涓�涓粯璁ょ殑option锛岄兘鍙互閫氳繃杩欎釜鏂规硶鍘诲垽鏂��
3. 鑾峰彇椤甸潰鍏冪礌澶у皬
鏈枃浠嬬粛濡備綍閫氳繃Selenium鏂规硶鍘昏幏鍙栭〉闈㈠厓绱犵殑澶у皬銆傝繖涓満鏅槸鍙兘鍦ㄩ渶瑕佹祴璇曠殑锛屼緥濡傚湪鍓嶇娴嬭瘯杩囩▼涓紝闇�瑕佸幓娴嬭瘯鏌愪竴涓帶浠舵槸鍚﹀拰璁捐澶у皬淇濇寔涓�鑷淬��
杩欓噷鐢ㄧ櫨搴﹂椤电殑“鐧惧害涓�涓�”杩欎釜鎸夐挳涓句緥锛屾墦鍗板嚭杩欎釜鎸夐挳鐨勫ぇ灏忋��
鐩稿叧鑴氭湰浠g爜濡備笅锛�
3.1 浠g爜瀹炵幇锛�
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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
4. 缁勫悎閿�-鍏ㄩ�夋枃瀛�
鏈枃寮�濮嬩粙缁峉elenium涓璌eys妯″潡涓嬬殑涓�浜涚粍鍚堬紝鎴栧揩鎹烽敭鐨勪娇鐢ㄣ�傚墠闈㈢殑涓�绡囨枃绔狅紝浠嬬粛浜嗗浣曟柊寮�涓�涓猼ab锛屼篃鏄敤鍒拌繖鍧楃殑鐭ヨ瘑銆�
鏈枃浠嬬粛鍏ㄩ�夊綋鍓嶉〉闈㈡枃瀛楋紝蹇嵎閿槸 Ctrl+a锛屼富瑕佽繕鏄敤send_keys()鏂规硶杈撳叆缁勫悎閿紝杩欓噷鐢ㄧ櫨搴﹂椤典妇渚嬨��
鐩稿叧鑴氭湰浠g爜濡備笅锛�
4.1 浠g爜瀹炵幇锛�
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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
4.4 鐧惧害棣栭〉濡備笅锛�
鐧惧害棣栭〉鐩稿綋浜庢垜浠寜浜嗛敭鐩樼殑缁勫悎閿紙Ctrl + A锛�
5. 缁勫悎閿�-閫�鏍奸敭鍒犻櫎鏂囧瓧
鍓嶉潰涓�绡囨枃绔犱粙缁嶄簡锛屽父瑙勬竻闄ゆ枃鏈緭鍏ユ瀛楃鐨勬柟娉昪lear(), 鏈枃浠嬬粛
鐩稿叧鑴氭湰浠g爜濡備笅锛岃繖閲岀敤鐧惧害棣栭〉鎼滅储杈撳叆妗嗕妇渚嬶細
5.1 浠g爜瀹炵幇锛�
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. 榧犳爣鍙抽敭
鏈枃浠嬬粛濡備綍浣跨敤Selenium涓彸閿柟娉曟潵鎿嶄綔缃戦〉鍏冪礌銆傜綉椤典笂鏈変簺鍏冪礌鏄敮鎸佸彸閿潵瑙﹀彂鏂扮殑鑿滃崟锛岃繖閲屾壘涓�涓櫨搴﹂椤碉紝鐧惧害logo鐨勫彸閿紝鏌ョ湅鍥剧墖鍔ㄤ綔鏉ユā鎷熻繖涓満鏅��
闂鎷嗗垎锛�
1. 鎵撳紑鐧惧害棣栭〉锛屾壘鍒發ogo锛屽彸閿紶鏍�
2. 绉诲姩鍒拌彍鍗曪紝鏌ョ湅鍥惧儚锛岀劧鍚庣偣鍑�
鏍稿績闂锛屽浣曞彸閿搷浣滐紝鐒跺悗鏄浣曢�夋嫨鏌ョ湅鍥惧儚杩欎釜鑿滃崟銆傚湪Selenium涓紝鏈変竴涓狝ctionChains妯″潡鏀寔锛屽彸閿紝榧犳爣鎮仠锛屾嫋鎷斤紝鍙屽嚮绛夊姩浣溿�傛垜浠彲浠ラ�氳繃閿洏鍚戜笅绠ご鏉ラ�夋嫨鏌ョ湅鍥惧儚杩欎釜鑿滃崟锛岀劧鍚庣偣鍑诲氨鍙互杈惧埌鐩殑銆�
鐩稿叧鑴氭湰浠g爜濡備笅锛�
6.1 浠g爜瀹炵幇锛�
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爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
娉ㄦ剰锛�
ActionChains涓嬬浉鍏虫柟娉曞湪褰撳墠鐨刦irefox涓嶅伐浣滐紝杩欎釜鏄竴涓凡鐭ョ殑bug銆�
7. 灏忕粨
濂戒簡锛屼粖澶╃殑缁冧範灏卞埌杩欓噷锛屽笇鏈涘ぇ瀹跺ソ濂界殑缁冧範鍜岀悊瑙c��
鎮ㄧ殑鑲畾灏辨槸鎴戣繘姝ョ殑鍔ㄥ姏銆�濡傛灉浣犳劅瑙夎繕涓嶉敊锛屽氨璇烽紦鍔变竴涓嬪惂锛佽寰楃偣娉� 鎺ㄨ崘 涓嶈蹇樿鍝︼紒锛侊紒