1. 绠�浠�
銆�銆�“鍘嬭酱”鍘熸湰鏄垙鏇插悕璇嶏紝鎸囦竴鍦烘姌瀛愭垙婕斿嚭鐨勫�掓暟绗簩涓墽鐩�傚湪鐜颁唬绀句細涓湁寰堝搴旂敤锛屾瘮濡�“鍘嬭酱鎴�”锛屼絾鍘嬭酱涔熸槸浜轰滑鐭ヨ瘑鐨勪竴涓洸鍖恒��“鍘嬭酱”鏈剰鏄寚鍊掓暟绗簩涓妭鐩紝鑰屼笉鏄汉浠父璇寸殑鍊掓暟绗竴涓紝鍊掓暟绗竴涓妭鐩О“鍘嬪彴”銆傛兂鐪�“鍘嬪彴绡�”锛屾暚璇峰叧娉ㄥ畯鍝�
绛夊緟鍘嬪彴绡囧彂甯冦�傚綋鐒朵簡锛屽畯鍝ヤ笉鏄敱鎴忓晩锛岃�屾槸鍒嗕韩鎶�鏈被鏂囩珷鐨勩�傚ソ浜嗚█褰掓浼犲洖褰掓棰樸��
銆�銆�涔嬪墠鐨勬枃绔犱腑锛岄拡瀵瑰厓绱犵殑鍚勭灞炴�э紝杩涜浜嗙浉瀵瑰簲鐨勬搷浣滐紝鑰屼笖瀵规ā鎷熼敭鐩樺拰榧犳爣鐨勬搷浣滀篃鏈夌畝鍗曚粙缁嶃�傞偅涔堟帴涓嬫潵鐢卞畯鍝ュ甫鐫�鍚勪綅灏忎紮浼村拰绔ラ瀷浠潵璇︾粏鍦扮湅鐪媠elenium濡備綍妯℃嫙鎿嶄綔閿洏鍜岄紶鏍囷紱缁冧範濡備綍鎵цJavaScript銆佸绐楀彛鍒囨崲銆佸鐞唅frame鍒囨崲绛夌煡
璇嗗拰鍐呭銆�
2. 閿洏浜嬩欢
webdriver鍙互妯℃嫙甯歌閿洏涓婃墍鏈夌殑鎸夐敭鎿嶄綔锛屽湪瀵煎叆鍖呯殑婧愮爜涓彲浠ョ湅鍒版墍鏈夊懡浠ゅ搴旂殑鎸夐敭銆�
浠ヤ唬鐮佺殑褰㈠紡鏉ュ睍绀猴細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-06 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: 銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�7-python+ selenium鑷姩鍖栨祴璇� ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys driver = webdriver.Chrome() driver.get("https://www.baidu.com") # 閿洏浜嬩欢 driver.find_element(By.ID, 'kw').send_keys("selenium&python?") time.sleep(3) driver.find_element(By.ID, 'kw').send_keys(Keys.BACK_SPACE) # 鍒╃敤backspace閿垹闄ゅ瓧绗︿覆鏈�鍚庝竴浣� time.sleep(3) driver.find_element(By.ID, 'kw').send_keys(Keys.SPACE) # 鍦ㄥ瓧绗︿覆鍚庣┖鏍奸敭娣诲姞绌烘牸 driver.find_element(By.ID, 'kw').send_keys("瀛︿範") # 缁х画鍦ㄥ瓧绗︿覆鍚庨潰娣诲姞瀛楃涓� driver.find_element(By.ID, 'kw').send_keys(Keys.ENTER) # 杈撳叆鍥炶溅enter閿繘琛屾悳绱� time.sleep(3) driver.find_element(By.ID, 'kw').send_keys(Keys.CONTROL, 'a') # 鍏ㄩ�夋悳绱㈡涓殑鍐呭 driver.find_element(By.ID, 'kw').send_keys(Keys.CONTROL, 'x') # 鍓垏鎼滅储妗嗕腑鐨勫唴瀹癸紝涔熷彲浠ヤ娇鐢╟杩涜澶嶅埗 time.sleep(3) # 鎵撳紑鍙﹀涓�涓悳绱㈢綉绔欙紝杈撳叆鍒氬垰鍓垏鐨勫唴瀹� driver.get("https://www.google.com") driver.find_element(By.ID, 'lst-ib').send_keys(Keys.CONTROL, 'v') # 绮樿创鍒氬垰澶嶅埗銆佸壀鍒囩殑鍐呭 driver.find_element(By.ID, 'lst-ib').submit() # 鎻愪氦鎼滅储 time.sleep(3) """ send_keys(Keys.BACK_SPACE) 鍒犻櫎閿紙BackSpace锛� send_keys(Keys.SPACE) 绌烘牸閿�(Space) send_keys(Keys.TAB) 鍒惰〃閿�(Tab) send_keys(Keys.ESCAPE) 鍥為��閿紙Esc锛� send_keys(Keys.ENTER) 鍥炶溅閿紙Enter锛� send_keys(Keys.CONTROL, ‘a’) 鍏ㄩ�夛紙Ctrl+A锛� send_keys(Keys.CONTROL, ‘c’) 澶嶅埗锛圕trl+C锛� send_keys(Keys.CONTROL, ‘x’) 鍓垏锛圕trl+X锛� send_keys(Keys.CONTROL, ‘v’) 绮樿创锛圕trl+V锛� send_keys(Keys.F1) 閿洏 F1 ... send_keys(Keys.F12) 閿洏 F12 """ driver.quit()
3. 榧犳爣浜嬩欢
涓婇潰鐨勯敭鐩樹簨浠舵瘮杈冨ソ鐞嗚В锛岄紶鏍囦簨浠剁殑璇濈洿鎺ョ湅婧愪唬鐮佹垨璁稿氨鏈変簺绾犵粨浜嗭紝浣嗘槸婧愪唬鐮佺殑娉ㄩ噴璇存槑鍏跺疄瀵笰ctionChains璇存槑鐨勬瘮杈冩竻妤氥��
鐢ㄦ埛鎿嶄綔銆�
鍦ˋctionChains瀵硅薄涓婅皟鐢ㄦ搷浣滄柟娉曟椂锛屾搷浣滃皢瀛樺偍鍦ˋctionChains瀵硅薄鐨勯槦鍒椾腑銆�
褰撹皟鐢╬erform()鏃讹紝浜嬩欢灏嗘寜瀹冧滑鎺掗槦鐨勯『搴忚Е鍙戙��
ActionChains鍙互鐢ㄤ簬閾惧紡鎴栬�呭彲浠ュ皢鎿嶄綔閫愪釜鎺掗槦锛岀劧鍚庢墽琛屻��
鏃犺鍝鏂瑰紡锛屽姩浣滈兘鎸夌収瀹冧滑琚皟鐢ㄧ殑椤哄簭鎵ц锛屼竴涓湪鍙︿竴涓箣鍚庢墽琛屻��
杩欓噷杩樻槸浠ヤ唬鐮佸睍绀洪儴鍒嗗姛鑳藉惂锛�
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-06 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: 銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�7-python+ selenium鑷姩鍖栨祴璇� ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.common.action_chains import ActionChains driver = webdriver.Chrome() driver.get("https://www.baidu.com") time.sleep(3) # 瀹氫綅鍏冪礌 ele_1 = driver.find_element(By.XPATH, '//*[@id="u1"]/a[9]') ele_2 = driver.find_element(By.XPATH, '//*[@id="u1"]/a[8]') # 灏嗛紶鏍囨偓鍋滃湪鍏冪礌1涓�3绉掑悗鍐嶆鎮仠鍒板厓绱�2涓� ActionChains(driver).move_to_element(ele_1).perform() time.sleep(3) ActionChains(driver).move_to_element(ele_2).perform() time.sleep(3) driver.quit()
涓婅堪浠g爜瀹炵幇浜嗗湪鍏冪礌涓婅繘琛屾偓鍋滐紝鍐嶇Щ鍔ㄨ嚦鍙︿竴鍏冪礌涓娿��
杩樻湁鍏朵粬鎿嶄綔濡備笅锛�
""" click(on_element=None) ——鍗曞嚮榧犳爣宸﹂敭 click_and_hold(on_element=None) ——鐐瑰嚮榧犳爣宸﹂敭锛屼笉鏉惧紑 context_click(on_element=None) ——鐐瑰嚮榧犳爣鍙抽敭 double_click(on_element=None) ——鍙屽嚮榧犳爣宸﹂敭 drag_and_drop(source, target) ——鎷栨嫿鍒版煇涓厓绱犵劧鍚庢澗寮� drag_and_drop_by_offset(source, xoffset, yoffset) ——鎷栨嫿鍒版煇涓潗鏍囩劧鍚庢澗寮� key_down(value, element=None) ——鎸変笅鏌愪釜閿洏涓婄殑閿� key_up(value, element=None) ——鏉惧紑鏌愪釜閿� move_by_offset(xoffset, yoffset) ——榧犳爣浠庡綋鍓嶄綅缃Щ鍔ㄥ埌鏌愪釜鍧愭爣 move_to_element(to_element) ——榧犳爣绉诲姩鍒版煇涓厓绱� move_to_element_with_offset(to_element, xoffset, yoffset) ——绉诲姩鍒拌窛鏌愪釜鍏冪礌锛堝乏涓婅鍧愭爣锛夊灏戣窛绂荤殑浣嶇疆 perform() ——鎵ц閾句腑鐨勬墍鏈夊姩浣� release(on_element=None) ——鍦ㄦ煇涓厓绱犱綅缃澗寮�榧犳爣宸﹂敭 send_keys(*keys_to_send) ——鍙戦�佹煇涓敭鍒板綋鍓嶇劍鐐圭殑鍏冪礌 send_keys_to_element(element, *keys_to_send) ——鍙戦�佹煇涓敭鍒版寚瀹氬厓绱� """
閫氳繃杩欎簺閿洏鍜岄紶鏍囩殑鎿嶄綔锛屽氨鍙互鍋氭洿澶氫簡
4. 鎵цJavaScript
鍓嶉潰鏂囩珷浠嬬粛浜咥ctionChains涓嬬殑榧犳爣鎮仠鍜屽彸閿搷浣滐紝鍏跺疄ActionChains杩樻湁鍏朵粬鏂规硶锛屼綘鍙互 action = ActionChains,閫氳繃杩欙紝鍏堝垵濮嬪寲涓�涓狝ctionChains瀹炰緥瀵硅薄锛岀劧鍚庡湪action鍚庨潰杈撳叆涓�涓偣鍙凤紝鏌ョ湅鏀寔鍝簺action鏂规硶銆傛湰鏂囧紑濮嬩粙缁嶅浣曟墽琛孞S鑴氭湰锛屼細鐢ㄤ袱涓緥瀛愯鏄庛��
绀轰緥涓�锛屾墽琛宩s鑴氭湰瑙﹀彂涓�涓猘lert寮瑰嚭妗嗐��
鐩稿叧鑴氭湰浠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 driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("https://www.baidu.com") time.sleep(1) driver.execute_script("window.alert('杩欐槸涓�涓猘lert寮规銆�');") # 娉ㄦ剰杩欓噷鐨勫垎鍙锋槸鑻辨枃杈撳叆娉曠殑鍒嗗彿锛屼笉鑳界敤涓枃
4.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
4.4 娴忚鍣ㄨ繍琛岀粨鏋�
涓轰簡鐪嬪緱娓呮锛屾垜浠繖閲岃繕鏄笉閫�鍑烘祻瑙堝櫒锛屾祻瑙堝櫒杩愯鍚庯紝濡備笅鍥撅細
4.5 绀轰緥浜�
鎵цjs鑴氭湰鏉ユ帶鍒舵祻瑙堝櫒绔栧悜婊氬姩鏉�
鎵撳紑鐧惧害璐村惂锛岀劧鍚庢嫋鍔ㄦ粴鍔ㄦ潯鍒板乏渚� “鍦板尯"
4.5.1 浠g爜瀹炵幇锛�
4.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 driver = webdriver.Chrome() driver.maximize_window() driver.implicitly_wait(6) driver.get("https://tieba.baidu.com/index.html") time.sleep(1) target_elem = driver.find_element_by_link_text("鍦板尯") driver.execute_script("return arguments[0].scrollIntoView();",target_elem) # 鐢ㄧ洰鏍囧厓绱犲弬鑰冨幓鎷栧姩 #driver.execute_script("scroll(0,2400)") # 杩欎釜鏄浜岀鏂规硶锛屾瘮杈冪矖鍔o紝澶ф鐨勬嫋鍔�
4.5.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
4.5.4 娴忚鍣ㄨ繍琛岀粨鏋�
涓轰簡鐪嬪緱娓呮锛屾垜浠繖閲岃繕鏄笉閫�鍑烘祻瑙堝櫒锛屾祻瑙堝櫒杩愯鍚庯紝濡備笅鍥撅細
5. 澶氱獥鍙d箣闂村垏鎹�
鏈枃鏉ヤ粙缁嶅浣曞鐞哾river鍦ㄥ绐楀彛涔嬮棿鍒囨崲锛屾兂涓�涓嬭繖鏍风殑鍦烘櫙锛屽湪椤甸潰A鐐瑰嚮涓�涓繛鎺ワ紝浼氳Е鍙戝湪鏂癟ab鎴栬�呮柊绐楀彛鎵撳紑椤甸潰B锛岀敱浜庝箣鍓嶇殑driver瀹炰緥瀵硅薄鍦ㄩ〉闈锛屼絾鏄綘鎺ヤ笅鏉ョ殑鑴氭湰鏄搷浣滈〉闈鐨勫厓绱狅紝杩欐牱灏遍�犳垚浜嗘壘涓嶅埌鍏冪礌鐨勬姤閿欍�傛湰鏉ヤ粙缁峴elenium涓璼witch_to.window()鏂规硶鏉ュ鐞嗚繖涓棶棰樸��
娴嬭瘯鍦烘櫙锛氭墦寮�鐧惧害鏂伴椈锛堥〉闈锛夛紝鐐瑰嚮鐑偣鏂伴椈涓涓�涓柊闂婚摼鎺ワ紙涓�鑸槸鍥藉棰嗗浜虹殑鏂伴椈锛夛紝浼氬湪绗簩涓獥鍙f墦寮�杩欎釜鏂伴椈鐨勫叿浣撹鎯呴〉锛堥〉闈锛夛紝娴嬭瘯闇�瑕佸幓鍒ゆ柇浣犵偣鍑昏繖涓繖涓柊闂伙紝鍦ㄦ墦寮�鐨勮鎯呴〉鏄惁姝g‘銆�
闂鎷嗗垎锛�
1. 鎴戜滑宸茬粡鐭ラ亾switch_to.window()鏂规硶鍙互澶勭悊绐楀彛鍒囨崲鐨勯棶棰�
2. 鍦ㄩ〉闈璺宠浆鍒伴〉闈涔嬪墠锛屾垜浠渶瑕佺敤涓�涓彉閲忎繚瀛樿繖涓柊闂荤殑鏍囬
3. 鍒囨崲鍒伴〉闈鍚庯紝鎴戜滑鑾峰彇杩欎釜鏂伴椈鏍囬锛岀劧鍚庡拰鍓嶉潰杩欎釜鍙橀噺淇濆瓨鐨勫�煎幓瀵规瘮锛屽鏋滅浉绛夛紝閭d箞灏辨祴璇曢�氳繃銆�
鎴戜滑鍒嗕袱涓楠ゅ幓瑙g瓟杩欎釜娴嬭瘯闇�姹傦細
1. 鍏堝疄鐜伴〉闈鍒囨崲鍒伴〉闈
2. 椤甸潰A鍜岄〉闈涓や釜鏂伴椈鏍囬杩涜瀵规瘮
鍏堢湅鐪嬬獥鍙e垏鎹㈢殑鑴氭湰浠g爜锛�
5.1 浠g爜瀹炵幇锛�
5.2 鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-06 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒5 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.get('http://news.baidu.com') time.sleep(1) driver.find_element_by_xpath("//*[@id='pane-news']/div/ul/li[1]/strong/a").click() print (driver.current_window_handle) # 杈撳嚭褰撳墠绐楀彛鍙ユ焺 handles = driver.window_handles # 鑾峰彇褰撳墠鍏ㄩ儴绐楀彛鍙ユ焺闆嗗悎 print (handles) # 杈撳嚭鍙ユ焺闆嗗悎 for handle in handles:# 鍒囨崲绐楀彛 if handle != driver.current_window_handle: print ('switch to second window',handle) driver.close() # 鍏抽棴绗竴涓獥鍙� driver.switch_to.window(handle) #鍒囨崲鍒扮浜屼釜绐楀彛
5.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
5.4 娴忚鍣ㄨ繍琛岀粨鏋�
涓轰簡鐪嬪緱娓呮锛屾垜浠繖閲岃繕鏄笉閫�鍑烘祻瑙堝櫒锛屾祻瑙堝櫒杩愯鍚庯紝濡備笅鍥撅細
浠g爜瑙i噴锛�
1. 鍏堟墦鍗板綋鍓嶇獥鍙o紙椤甸潰A锛夊彞鏌勶紝涓�涓暱瀛楃涓�
2. 鑾峰彇鍏ㄩ儴绐楀彛鍙ユ焺锛岃繖閲屾垜浠湁涓や釜锛屾墦鍗板嚭杩欎袱涓彞鏌勩��
3. for 寰幆閬嶅巻锛屽綋鍙戠幇鍏朵腑涓�涓彞鏌勫拰褰撳墠鍙ユ焺涓嶇浉绛夛紝杩欓噷鍋氫簡涓変欢浜嬶紝绗竴浠讹紝鎵撳嵃涓�涓鍙ワ紝鍛婅瘔浣犻┈涓婅鍒囨崲鍒扮浜屼釜绐楀彛锛岀浜屼欢锛屽叧闂綋鍓嶇獥鍙o紝杩欓噷鎸囬〉闈锛岀涓夛紝鍒囨崲鍒伴〉闈绐楀彛銆�
杩欓噷鍦ㄥ垏鎹㈠埌绗簩涓〉闈㈡椂鍊欙紝鎴戝叧闂箣鍓嶇殑椤甸潰A,鍙槸鎴戜釜浜烘祴璇曚範鎯紝浣犲彲浠ヤ笉鍏抽棴锛屼絾鏄綘鐨勮鎬濊矾娓呮櫚锛屽摢浜涘厓绱犲湪椤甸潰A杩樻槸鍦ㄩ〉闈锛屽鏋滄搷浣滀簡椤甸潰B鍚庤繕瑕佹搷浣滈〉闈鐨勫厓绱狅紝浣犺繕瑕佸垏鎹㈠埌椤甸潰A锛屼负浜嗛夯鐑︼紝寤鸿浣犳瘡娆″垏鎹㈤兘鎶婂墠闈㈤〉闈㈢粰鍏抽棴銆�
5.4 鏂█鍒囨崲鐨勬槸涓嶆槸浣犲垰鍒氱偣鍑荤殑鏂伴椈
鍒ゆ柇鍦ㄦ墦寮�鏂伴〉闈㈡樉绀虹殑鏂伴椈鏍囬鏄笉鏄綘鍒氬垰鐐瑰嚮鐨勬柊闂伙紝鑴氭湰浠g爜濡備笅锛�
5.4.1 浠g爜瀹炵幇锛�
5.4.2鍙傝�冧唬鐮侊細
# coding=utf-8馃敟 # 1.鍏堣缃紪鐮侊紝utf-8鍙敮鎸佷腑鑻辨枃锛屽涓婏紝涓�鑸斁鍦ㄧ涓�琛� # 2.娉ㄩ噴锛氬寘鎷褰曞垱寤烘椂闂达紝鍒涘缓浜猴紝椤圭洰鍚嶇О銆� ''' Created on 2019-12-06 @author: 鍖椾含-瀹忓摜 QQ浜ゆ祦缇わ細705269076 Project: python+ selenium鑷姩鍖栨祴璇曠粌涔犵瘒5 ''' # 3.瀵煎叆妯″潡 import time from selenium import webdriver driver = webdriver.Chrome() driver.maximize_window() driver.get('http://news.baidu.com') time.sleep(1) news_link = driver.find_element_by_xpath("//*[@id='pane-news']/div/ul/li[1]/strong/a") page1_title_string = news_link.text #寰楀埌椤甸潰A鏂伴椈鏍囬 print('page1_title_string'+ page1_title_string) news_link.click() # 鐐瑰嚮鏂伴椈閾炬帴 time.sleep(1) handles = driver.window_handles for handle in handles:# 鍒囨崲绐楀彛锛堝垏鎹㈠埌鎼滅嫍锛� if handle != driver.current_window_handle: print ('switch to second window',handle) driver.close() # 鍏抽棴绗竴涓獥鍙� driver.switch_to.window(handle) #鍒囨崲鍒扮浜屼釜绐楀彛 page2_title_string = driver.find_element_by_xpath(".//*[@id='title_area']/h1").text # 璇︽儏椤垫湁涓�涓師鏍囬 print('page2_title_string'+ page2_title_string) try : assert page1_title_string in page2_title_string # 鍒ゆ柇椤甸潰B鏍囬鏄惁鍖呭惈椤甸潰A鏍囬 print ('Test Pass.') except Exception as e: print ('Test Fail')
5.4.3 杩愯缁撴灉锛�
杩愯浠g爜鍚庯紝鎺у埗鍙版墦鍗板涓嬪浘鐨勭粨鏋�
娉ㄦ剰锛氬緢鏈夊彲鑳介亣鍒帮紝椤甸潰A鐨勬爣棰樻槸绠�鐭紝椤甸潰B鐨勬爣棰樻槸闀挎爣棰橈紝杩欎釜鏃跺�欏氨闇�瑕侀噰鍙栵紝瀛楃涓插寘鍚殑鍏崇郴鍘绘柇瑷�锛屾垜杩欓噷鍒氬ソ鎵撳紑鐨勮鎯呴〉鏈変竴涓彨 “鍘熸爣棰橈細xxxx”锛岀敱浜庤繖涓柊闂诲叿鏈夋椂鏁堟�э紝绛変綘鐪嬭繖绡囨枃绔狅紝鍙兘鑴氭湰杩愯涓嶆垚鍔燂紝闇�瑕佽皟鏁翠笅鑴氭湰锛屽鏋滀笉
濂芥柇瑷�锛屽氨鏀惧純鏂█閮ㄥ垎锛屾湰鏂囧彧瀛︾獥鍙e垏鎹紝鑷充簬娴嬭瘯鏂█鎶�鑳斤紝闇�瑕佷綘涓嶆柇鍘诲啓鑴氭湰锛屽幓鎺屾彙鍜屾彁楂樼殑閮ㄥ垎銆�
6. 灏忕粨
濂戒簡锛屼粖澶╃殑缁冧範灏卞埌杩欓噷锛屽笇鏈涘ぇ瀹跺ソ濂界殑缁冧範鍜岀悊瑙c��
鎮ㄧ殑鑲畾灏辨槸鎴戣繘姝ョ殑鍔ㄥ姏銆�濡傛灉浣犳劅瑙夎繕涓嶉敊锛屽氨璇烽紦鍔变竴涓嬪惂锛佽寰楃偣娉� 鎺ㄨ崘 涓嶈蹇樿鍝︼紒锛侊紒