銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�

1. 绠�浠�

 銆�銆�鏈潵涓婁竴绡囧氨鏄粌涔犵瘒鐨勬渶鍚庝竴绡囨枃绔犱簡锛屼絾鏄湁鐨勫皬浼欎即绉佷笅鍙嶆槧璇存槸鍋氫簡閭d箞澶氱粌涔狅紝娌℃湁涓�涓瘮杈冪患鍚堢殑demo缁冪粌鎵嬨�傚洜姝ゅ畯鍝ュ湪杩欓噷鍙堣ˉ瀛樹簡涓�浜涘父瑙佺殑鐭ヨ瘑鐐硅繘琛岀粌涔狅紝鍦ㄦ枃绔犳渶鍚庝篃閫氳繃瀹炰緥缁欏皬浼欎即浠垨鑰呯闉嬩滑杩涜浜嗕竴涓櫥褰曟ā鍧楃殑鑷姩鍖栨祴璇曠殑瀹炰緥锛屽叾浠栫殑浣犲彲浠ョ収鐚敾铏庡湴杞绘澗鐨勬悶瀹氬拰瀹炵幇銆�

2. python-web鑷姩鍖�-涓夌绛夊緟鏂瑰紡

褰撴湁鍏冪礌瀹氫綅涓嶅埌鏃讹紝姣斿涓嬫媺妗嗭紝寮瑰嚭妗嗙瓑鍚勭瀹氫綅涓嶅埌鏃讹紱

涓�鑸槸涓ょ闂锛�1 銆佹湁frame 锛�2銆佹病鏈夊姞绛夊緟

涓嬮潰瀛︿範涓夌绛夊緟鏂瑰紡锛�

2.1 寮哄埗绛夊緟 sleep锛坸x锛夋垨鑰匱ime.sleep

鏄湪绋嬪簭杩愯杩囩▼涓娇鐢╰ime妯″潡涓殑sleep杩涜浠g爜鐨勪紤鐪犺繘琛屽己鍒剁瓑寰咃紝鏄樉寮忕瓑寰呬腑鐨勪竴绉嶆瀬绔儏鍐点��

杩欑鏂规硶绠�鍗曠矖鏆达紝涓嶇娴忚鍣ㄦ槸鍚﹀姞杞藉畬锛岀▼搴忛兘瑕佺瓑寰呰瀹氱殑xx鏃堕棿锛屾椂闂村埌浜嗘墠缁х画鎵ц涓嬮潰鐨勪唬鐮併��

涓嶅缓璁�绘槸鐢ㄨ繖绉嶇瓑寰呮柟寮忥紝浼氫弗閲嶅奖鍝嶇▼搴忕殑鎵ц閫熷害銆傞�氳繃time妯″潡涓璼leep杩涜浠g爜鐨勬殏鍋滐紝浣嗘槸瀹為檯浣跨敤杩囩▼涓紝濡傛灉閮戒互sleep杩涜鎺у埗涓ラ噸褰卞搷浜嗙▼搴忕殑杩愯銆�

2.1.1 鍙傝�冧唬鐮�1
# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

import time

# 寮哄埗绛夊緟——浠g爜浼戠湢

from selenium import webdriver

driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(3)

driver.quit()
2.1.2 鍙傝�冧唬鐮�2
# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

from selenium import webdriver
from time import sleep
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
sleep(3)#寮哄埗鎬х瓑寰�3s鍐嶆墽琛屼互涓嬩唬鐮�
print(driver.current_url)
driver.quit()#閫�鍑洪┍鍔紝鍏抽棴鎵�鏈夌獥鍙�

2.2 闅愭�х瓑寰� implicitly_wait(xx)

杩欑鏂规硶鏄缃竴涓渶闀跨殑绛夊緟鏃堕棿锛屽鏋滃湪瑙勫畾鏃堕棿鍐呯綉椤靛叏閮ㄥ厓绱犲姞杞藉畬鎴愶紝鍒欐墽琛屼笅涓�姝ワ紝鍚﹀垯涓�鐩寸瓑寰呮椂闂存埅姝㈡墠鎵ц涓嬩竴姝ャ�傛瘮寮哄埗鎬х瓑寰呮櫤鑳戒簺

锛侀殣鎬х瓑寰呭鏁翠釜driver鐨勫懆鏈熼兘璧蜂綔鐢紝鎵�浠ュ彧瑕佽缃竴娆″嵆鍙�

2.2.1 鍙傝�冧唬鐮�1
# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from time import ctime

driver = webdriver.Firefox()

# 璁剧疆闅愬紡绛夊緟涓�10绉�
driver.implicitly_wait(10)
driver.get("http://www.baidu.com") try: print(ctime()) driver.find_element_by_id("kw22").send_keys('selenium') except NoSuchElementException as e: print(e) finally: print(ctime()) driver.quit()

杩欓噷鍙互鐪嬪埌鍦�10绉掑唴娌℃湁鎵惧埌鎯宠鎵惧埌鐨勫厓绱狅紝浣嗘槸渚濇棫鎵ц浜�10绉掞紝鐒跺悗鎶ラ敊锛屽鏋滀慨鏀逛唬鐮佷负鍙互鎵惧埌锛屼唬鐮佹墽琛岄潪甯歌繀閫熴��

implicitly_wait()榛樿鍙傛暟鐨勫崟浣嶄负绉掞紝鏈緥涓缃瓑寰呮椂闀夸负10绉掋�傞鍏堣繖10绉掑苟闈炰竴涓浐瀹氱殑绛夊緟鏃堕棿锛屽畠骞朵笉褰卞搷鑴氭湰鐨勬墽琛岄�熷害銆傚叾娆★紝瀹冨苟涓嶉拡瀵归〉闈笂鐨勬煇涓�鍏冪礌杩涜绛夊緟銆傚綋鑴氭湰鎵ц鍒版煇涓厓绱犲畾浣嶆椂锛屽鏋滃厓绱犲彲浠ュ畾浣嶏紝鍒欑户缁墽琛岋紱濡傛灉鍏冪礌瀹氫綅

涓嶅埌锛屽垯瀹冨皢浠ヨ疆璇㈢殑鏂瑰紡涓嶆柇鍦板垽鏂厓绱犳槸鍚﹁瀹氫綅鍒般�傚亣璁惧湪绗�6绉掑畾浣嶅埌浜嗗厓绱犲垯缁х画鎵ц锛岃嫢鐩村埌瓒呭嚭璁剧疆鏃堕暱锛�10绉掞級杩樻病鏈夊畾浣嶅埌鍏冪礌锛屽垯鎶涘嚭寮傚父銆�

2.2.2 鍙傝�冧唬鐮�2
# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

from selenium import webdriver
driver = webdriver.Chrome()
driver.get('https://www.baidu.com/')
driver.implicitly_wait(30)#闅愭�х瓑寰�,鏈�闀�30s
print(driver.current_url)
driver.quit()

2.3.鏄炬�х瓑寰� WebDriverWait

WebDriverWait閰嶅悎璇ョ被鐨剈ntil()鍜寀ntil_not()鏂规硶锛屾牴鎹潯浠剁伒娲荤殑绛夊緟

绋嬪簭姣忛殧xx绉掔湅涓�鐪硷紝濡傛灉鏉′欢鎴愮珛浜嗭紝鍒欐墽琛屼笅涓�姝ワ紝鍚﹀垯缁х画绛夊緟锛岀洿鍒拌秴杩囪缃殑鏈�闀挎椂闂达紝鐒跺悗鎶涘嚭TimeoutException銆�

鏄惧紡绛夊緟鏄綘鍦ㄤ唬鐮佷腑瀹氫箟绛夊緟涓�瀹氭潯浠跺彂鐢熷悗鍐嶈繘涓�姝ユ墽琛屼綘鐨勪唬鐮併��

A. 浣跨敤鍓嶏紝鍏堝紩鐢ㄧ浉鍏冲簱

B. 纭畾鍏冪礌鐨勫畾浣嶈〃杈惧紡

C.  浣跨敤expected_conditions瀵瑰簲鐨勬柟娉曟潵鐢熸垚鍒ゆ柇鏉′欢

WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.ID,ele_locator)))
WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.XPATH,ele_locator)))

D.  璋冪敤WebDriverWait绫昏缃瓑寰呮�绘椂闀裤�佽疆璇㈠懆鏈�

2.3.1 鍙傝�冧唬鐮�1
# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

#A. 浣跨敤鍓嶏紝鍏堝紩鐢ㄧ浉鍏冲簱
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
 
driver = webdriver.Chrome()#鎵撳紑Chrome娴忚鍣�
driver.get('https://www.baidu.com/')#鎵撳紑鐧惧害
driver.find_element_by_xpath('//div[@id="u1"]//a[@name="tj_login"]').click()#鐐瑰嚮銆愮櫥褰曘�戯紱click() 鏂规硶锛屽彲妯℃嫙鍦ㄦ寜閽笂鐨勪竴娆¢紶鏍囧崟鍑汇��
# B. 纭畾鍏冪礌鐨勫畾浣嶈〃杈惧紡
ele_locator = "TANGRAM__PSP_10__footerULoginBtn"#閫氳繃id,纭畾‘鐢ㄦ埛鍚嶇櫥褰�’鍏冪礌
 
# C.  浣跨敤expected_conditions瀵瑰簲鐨勬柟娉曟潵鐢熸垚鍒ゆ柇鏉′欢
# EC.鏂规硶鍚�(瀹氫綅鏂瑰紡,瀹氫綅琛ㄨ揪寮�)
# EC.visibility_of_element_located(By.ID,ele_locator)#鍏冪礌鍙
 
# D.  璋冪敤WebDriverWait绫昏缃瓑寰呮�绘椂闀裤�佽疆璇㈠懆鏈�
# WebDriverWait(driver, 瓒呮椂鏃堕暱, 璋冪敤棰戠巼锛堥粯璁�0.5s锛�).until(鍙墽琛屾柟娉�, 瓒呮椂鏃惰繑鍥炵殑淇℃伅)
# 绛夊緟10绉掗挓锛屾瘡闅�1绉掑幓鏌ョ湅瀵瑰簲鐨勫厓绱犳槸鍚﹀彲瑙侊紱濡傛灉鍙锛岀户缁笅涓�姝ユ搷浣滐紱濡傛灉涓嶅彲瑙侊紝鍒欑户缁瓑寰咃紝鐩村埌10s缁撴潫锛屽鏋滃厓绱犺繕鏄笉鍙锛屽垯鎶涘嚭瓒呮椂寮傚父
WebDriverWait(driver,10,1).until(EC.visibility_of_element_located((By.ID,ele_locator)))

driver.find_element_by_id('TANGRAM__PSP_10__footerULoginBtn').click()#鐐瑰嚮銆愮敤鎴峰悕鐧诲綍銆�
 
driver.close()#鍏抽棴褰撳墠绐楀彛
2.3.2 鍙傝�冧唬鐮�2
# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

from time import ctime
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

dr = webdriver.Chrome() dr.get('https://www.baidu.com') try: print(ctime()) element = WebDriverWait(dr, 10).until( EC.presence_of_element_located((By.ID, "kw")) ) # WebDriverWait(driver=self.driver, timeout=300, poll_frequency=0.5, ignored_exceptions=None) # driver锛氭祻瑙堝櫒椹卞姩 # timeout锛氭渶闀胯秴鏃剁瓑寰呮椂闂� # poll_frequency锛氭娴嬬殑鏃堕棿闂撮殧锛岄粯璁や负500ms # ignore_exception锛氳秴鏃跺悗鎶涘嚭鐨勫紓甯镐俊鎭紝榛樿鎯呭喌涓嬫姏 NoSuchElementException 寮傚父 print("鎴戝凡鎵惧埌") finally: print(ctime()) dr.quit()

鎵ц缁撴灉锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第1张图片

浠ヤ笂浠g爜鎵ц鍚庡氨鍙戠幇锛屾暣娈典唬鐮佹墽琛岄�熷害闈炲父蹇紝鍗充娇鎴戝湪WebDriverWait涓缃�10绉掞紝涔熶笉浼氱瓑寰�10绉掔殑鎯呭喌锛屽洜涓哄湪涓嶅埌涓�绉掑唴锛屽凡缁忓畬鎴愪簡鍔犺浇骞跺畾浣峣d涓�“kw”鐨勫厓绱犮��

閫氳繃WebDriverWait 鍜� ExpectedCondition 缁勫悎浣跨敤锛岃鎴戜滑鐨勪唬鐮佹墽琛屽彧闇�瑕佺瓑寰呴渶瑕佺殑鏃堕暱锛岃�屼笉鏄浐瀹氱殑鏃堕暱锛岃繖鏍锋渶澶ч檺搴︾殑鑺傜渷鏃堕棿銆�

 姝ゅExpectedCondition绫讳腑鎻愪緵浜嗗緢澶氶鏈熸潯浠跺垽鏂柟娉曪紝鐪佸幓浜嗗啀鍒涘缓鍖呯殑鍔熷か锛�

"""
title_is锛氬垽鏂綋鍓嶉〉闈㈢殑title鏄惁绛変簬棰勬湡
title_contains锛氬垽鏂綋鍓嶉〉闈㈢殑title鏄惁鍖呭惈棰勬湡瀛楃涓�
presence_of_element_located锛氬垽鏂煇涓厓绱犳槸鍚﹁鍔犲埌浜哾om鏍戦噷锛屽苟涓嶄唬琛ㄨ鍏冪礌涓�瀹氬彲瑙�
visibility_of_element_located锛氬垽鏂煇涓厓绱犳槸鍚﹀彲瑙�. 鍙浠h〃鍏冪礌闈為殣钘忥紝骞朵笖鍏冪礌鐨勫鍜岄珮閮戒笉绛変簬0
visibility_of锛氳窡涓婇潰鐨勬柟娉曞仛涓�鏍风殑浜嬫儏锛屽彧鏄笂闈㈢殑鏂规硶瑕佷紶鍏ocator锛岃繖涓柟娉曠洿鎺ヤ紶瀹氫綅鍒扮殑element灏卞ソ浜�
presence_of_all_elements_located锛氬垽鏂槸鍚﹁嚦灏戞湁1涓厓绱犲瓨鍦ㄤ簬dom鏍戜腑銆備妇涓緥瀛愶紝濡傛灉椤甸潰涓婃湁n涓厓绱犵殑class閮芥槸'column-md-3'锛岄偅涔堝彧瑕佹湁1涓厓绱犲瓨鍦紝杩欎釜鏂规硶灏辫繑鍥濼rue
text_to_be_present_in_element锛氬垽鏂煇涓厓绱犱腑鐨則ext鏄惁 鍖呭惈 浜嗛鏈熺殑瀛楃涓�
text_to_be_present_in_element_value锛氬垽鏂煇涓厓绱犱腑鐨剉alue灞炴�ф槸鍚﹀寘鍚簡棰勬湡鐨勫瓧绗︿覆
frame_to_be_available_and_switch_to_it锛氬垽鏂frame鏄惁鍙互switch杩涘幓锛屽鏋滃彲浠ョ殑璇濓紝杩斿洖True骞朵笖switch杩涘幓锛屽惁鍒欒繑鍥濬alse
invisibility_of_element_located锛氬垽鏂煇涓厓绱犱腑鏄惁涓嶅瓨鍦ㄤ簬dom鏍戞垨涓嶅彲瑙�
element_to_be_clickable - it is Displayed and Enabled锛氬垽鏂煇涓厓绱犱腑鏄惁鍙骞朵笖鏄痚nable鐨勶紝杩欐牱鐨勮瘽鎵嶅彨clickable
staleness_of锛氱瓑鏌愪釜鍏冪礌浠巇om鏍戜腑绉婚櫎锛屾敞鎰忥紝杩欎釜鏂规硶涔熸槸杩斿洖True鎴朏alse
element_to_be_selected锛氬垽鏂煇涓厓绱犳槸鍚﹁閫変腑浜�,涓�鑸敤鍦ㄤ笅鎷夊垪琛�
element_located_to_be_selected
element_selection_state_to_be锛氬垽鏂煇涓厓绱犵殑閫変腑鐘舵�佹槸鍚︾鍚堥鏈�
element_located_selection_state_to_be锛氳窡涓婇潰鐨勬柟娉曚綔鐢ㄤ竴鏍凤紝鍙槸涓婇潰鐨勬柟娉曚紶鍏ュ畾浣嶅埌鐨別lement锛岃�岃繖涓柟娉曚紶鍏ocator
alert_is_present锛氬垽鏂〉闈笂鏄惁瀛樺湪alert
"""

鍙﹀杩欓噷浣跨敤浜唘ntil()鍑芥暟涔熷彲浠ヤ娇鐢╱ntil_not()

銆�銆�until_not(method, message='')

銆�銆�璋冪敤璇ユ柟娉曚綋鎻愪緵鐨勫洖璋冨嚱鏁颁綔涓轰竴涓弬鏁帮紝鐩村埌杩斿洖鍊间负False

銆�銆�until(method, message='')

銆�銆�璋冪敤璇ユ柟娉曚綋鎻愪緵鐨勫洖璋冨嚱鏁颁綔涓轰竴涓弬鏁帮紝鐩村埌杩斿洖鍊间负True

3. 涓嬫媺妗唎r寮规

閫氬父鎯呭喌涓嬶紝鍦ㄧ綉椤典腑浼氭湁涓�浜涗笅鎷夋杩涜閫夋嫨鎴栬�呯粰浣犱竴涓脊妗嗗憡璇変綘闇�瑕佽繘琛岀‘璁わ紝閬囧埌杩欐牱鐨勬儏鍐靛浣曡В鍐冲憿锛屽畯鍝ュ湪杩欓噷灏辨潵绠�鍗曞湴璇翠竴璇�……‘’

3.1 浠g爜瀹炵幇锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第2张图片

3.2 鍙傝�冧唬鐮侊細

# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

import time

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.select import Select


driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
time.sleep(5)

# driver.find_element(By.ID, "kw").send_keys("selenium&python")
# driver.find_element(By.ID, "su").click()
# driver.find_element(By.XPATH, '/html/body/div[1]/div[5]/div[1]/div[2]/div[1]/div[2]/div[1]').click()
# driver.find_element(By.XPATH, '/html/body/div[1]/div[5]/div[1]/div[2]/div[1]/div[1]/span[2]').click()
# 鎶樿吘鍗婂ぉ鎵嶅彂鐜板畾浣嶅嚭鏉ョ殑涓嶆槸涓嬫媺妗�……
driver.find_element(By.LINK_TEXT, '璁剧疆').click()
driver.find_element(By.LINK_TEXT, '鎼滅储璁剧疆').click()
time.sleep(2)

sel = driver.find_element(By.XPATH, '//*[@id="nr"]')  # 瀹氫綅涓嬫媺妗�
Select(sel).select_by_value('50')  # 閫氳繃value鐨勫�艰繘琛岄�夊畾鏉$洰
driver.find_element(By.CLASS_NAME, 'prefpanelgo').click()
time.sleep(2)
driver.switch_to.alert.accept()  # 姝ゅ鍚屾剰璀﹀憡妗嗘彁绀哄唴瀹�
time.sleep(2)

driver.quit()

3.3 杩愯缁撴灉锛�

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

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第3张图片

3.4 娴忚鍣�

娴忚鍣ㄨ烦杞埌璁剧疆椤甸潰锛屼細鎻愮ず“宸茶褰曚笅鎮ㄧ殑鍋忓ソ”

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第4张图片

鍦ㄧ櫨搴﹂椤垫兂瑕佽繘琛岃缃瘡娆℃悳绱㈤〉闈㈡樉绀虹殑鏁扮洰锛屽氨浼氭湁涓�涓笅鎷夋杩涜閫夋嫨锛岃繖閲屽彲浠ュ鍏elect杩欎釜妯″潡杩涜瀵逛笅鎷夋杩涜閫夋嫨锛岄�氬父涓嬫媺妗嗙殑鍒楄〃鏄寘鍚竴涓�肩殑锛岃繖鏍峰氨鍙互閫氳繃value鏉ラ�夊畾锛屽畬鎴愯缃箣鍚庯紝褰撶劧鏄淇濆瓨璁剧疆鍟︼紝鐐瑰嚮淇濆瓨鍙戠幇锛屽眳鐒舵湁

涓�涓脊妗嗭紝杩欏彲濡備綍鏄ソ锛屽垏鎹㈣嚦alert涔嬪悗鎺ュ彈鍚�……

閫氳繃鍦ㄧ紪杈戝櫒涓殑鎻愮ず锛屾垜浠悓鏍峰彲浠ョ粰寮规鍙戦�佷竴涓�硷紝褰撶劧杩欓噷璁剧疆涓苟娌℃湁鍑虹幇杩欑被鎯呭喌銆�

杩欐瀵逛笅鎷夋鍜屽脊妗嗙殑澶勭悊杩囩▼涓紝鎶樿吘浜嗕笉鐭椂闂达紝鍘熷洜灏辨槸鍏冪礌绛夊緟鐨勯棶棰橈紝涓嬪洖瑕佷粩缁嗙爺绌朵笅鍏冪礌绛夊緟鐨勯棶棰樹簡銆�

4. 鑷姩鍖栨祴璇昫emo

浣跨敤selenium杩涜娴嬭瘯锛屽綋鐒舵槸闇�瑕佽繘琛岄獙璇佺殑锛屾鏃剁粨鍚坧ython鐨勬柇瑷�鍑芥暟灏卞彉寰楅潪甯告湁鐢ㄤ簡銆傛祴璇曚腑璁惧畾濂介鏈燂紝褰撳疄闄呮儏鍐典笌棰勬湡鏈夊樊鍒紝缁欏嚭閿欒淇℃伅锛屽ソ鍍忔鏄墍甯屾湜鐨勩��

鏉ヤ竴娈电畝鍗曠殑浠g爜鐪嬩笅鏂█鐨勫熀鏈娇鐢�:

澶嶅埗浠g爜
# coding:utf-8

import time

from selenium import webdriver dr = webdriver.Chrome() dr.maximize_window() dr.get("https://www.baidu.com") print(dr.title) try: assert (dr.title == "鐧惧害锛屼綘灏辩煡閬�") except: print('鏍囬閿欒锛岃鏌ョ湅閿欒淇℃伅') time.sleep(5) dr.quit()
澶嶅埗浠g爜

鑾峰彇椤甸潰鐨則itle鏄惁绗﹀悎鎴戜滑鐨勯鏈燂紝褰撴墦寮�鐨勭綉椤祎itle涓嶆槸鎴戜滑鎯宠鐨勶紝涓嶄粎浠庡嚱鏁癮ssert鐨勬彁绀轰俊鎭湅鍒板師鍥狅紝涔熷彲杈撳嚭绠�鍗曠殑鎻愮ず淇℃伅銆�

Web鐧诲綍娴嬭瘯鏄緢甯歌鐨勬祴璇曪紝鎵嬪姩娴嬭瘯澶у鍐嶇啛鎮変笉杩囦簡锛岄偅濡備綍杩涜鑷姩鍖栫櫥褰曟祴璇曞憿锛佹湰鏂囧氨鍩轰簬python+selenium缁撳悎unittest鍗曞厓娴嬭瘯妗嗘灦鏉ヨ繘琛屼竴娆$畝鍗曚絾姣旇緝瀹屾暣鐨刢nblog鑷姩鍖栫櫥褰曟祴璇曪紝鍙彁渚涚偣鍙傝�冿紒涓嬮潰灏卞寘鎷祴璇曚唬鐮佸拰姣忕娴嬭瘯鎯呭喌鐨勬埅鍥俱��

 鍙︿竴绉嶆柟寮忔槸缁撳悎python娴嬭瘯妗嗘灦unittest杩涜楠岃瘉銆備互涓嬬殑浠g爜鍙槸涓轰簡婕旂ず锛�

# coding=utf-8馃敟

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

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

# 3.瀵煎叆妯″潡

import unittest
from selenium import webdriver
from time import sleep
'''
cnblog鐨勭櫥褰曟祴璇曪紝鍒嗕笅闈㈠嚑绉嶆儏鍐碉細
(1)鐢ㄦ埛鍚嶃�佸瘑鐮佹纭�
(2)鐢ㄦ埛鍚嶆纭�佸瘑鐮佷笉姝g‘
(3)鐢ㄦ埛鍚嶆纭�佸瘑鐮佷负绌�
(4)鐢ㄦ埛鍚嶉敊璇�佸瘑鐮佹纭�
(5)鐢ㄦ埛鍚嶄负绌恒�佸瘑鐮佹纭紙杩樻湁鐢ㄦ埛鍚嶅拰瀵嗙爜鍧囦负绌烘椂涓庢鎯呭喌鏄竴鏍风殑锛岃繖閲屽氨涓嶅崟鐙祴璇曚簡锛�
'''

class LoginCase(unittest.TestCase):

    def setUp(self):
        self.dr = webdriver.Chrome()
        self.dr.maximize_window()

    # 瀹氫箟鐧诲綍鏂规硶
    def login(self, username, password):
        self.dr.get('https://passport.cnblogs.com/user/signin')  # cnblog鐧诲綍椤甸潰
        self.dr.find_element_by_id('LoginName').send_keys(username)
        self.dr.find_element_by_id('Password').send_keys(password)
        self.dr.find_element_by_id('submitBtn').click()

    def test_login_success(self):
        '''鐢ㄦ埛鍚嶃�佸瘑鐮佹纭�'''
        self.login('鍖椾含-瀹忓摜', '!qaz2wsx')  # 姝g‘鐢ㄦ埛鍚嶅拰瀵嗙爜
        sleep(3)
        link = self.dr.find_element_by_id('lnk_current_user')
        self.assertTrue('鍖椾含-瀹忓摜' in link.text)  # 鐢╝ssertTrue(x)鏂规硶鏉ユ柇瑷�  bool(x) is True 鐧诲綍鎴愬姛鍚庣敤鎴锋樀绉板湪lnk_current_user閲�
        self.dr.get_screenshot_as_file("D:\\cnblogtest\\login_success.jpg")  # 鎴浘  鍙嚜瀹氫箟鎴浘鍚庣殑淇濆瓨浣嶇疆鍜屽浘鐗囧懡鍚�

    def test_login_pwd_error(self):
        '''鐢ㄦ埛鍚嶆纭�佸瘑鐮佷笉姝g‘'''
        self.login('鍖椾含-瀹忓摜', 'kemi')  # 姝g‘鐢ㄦ埛鍚嶏紝閿欒瀵嗙爜
        sleep(2)
        error_message = self.dr.find_element_by_class_name('ajax-error-box').text
        self.assertIn('鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒', error_message)  # 鐢╝ssertIn(a,b)鏂规硶鏉ユ柇瑷� a in b  '鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒'鍦╡rror_message閲�
        self.dr.get_screenshot_as_file("D:\\cnblogtest\\login_pwd_error.jpg")

    def test_login_pwd_null(self):
        '''鐢ㄦ埛鍚嶆纭�佸瘑鐮佷负绌�'''
        self.login('鍖椾含-瀹忓摜', '')  # 瀵嗙爜涓虹┖
        error_message = self.dr.find_element_by_id('Password-error').text
        self.assertEqual(error_message, '璇疯緭鍏ュ瘑鐮�')  # 鐢╝ssertEqual(a,b)鏂规硶鏉ユ柇瑷�  a == b  璇疯緭鍏ュ瘑鐮佺瓑浜巈rror_message
        self.dr.get_screenshot_as_file("D:\\cnblogtest\\login_pwd_null.jpg")

    def test_login_user_error(self):
        '''鐢ㄦ埛鍚嶉敊璇�佸瘑鐮佹纭�'''
        self.login('鍖椾含-瀹忓摜1', '!qaz2wsx')  # 瀵嗙爜姝g‘锛岀敤鎴峰悕閿欒
        sleep(2)
        error_message = self.dr.find_element_by_id('ajax-error-box').text
        self.assertIn('鐢ㄦ埛鍚嶆垨瀵嗙爜閿欒', error_message)  # 鐢╝ssertIn(a,b)鏂规硶鏉ユ柇瑷� a in b
        self.dr.get_screenshot_as_file("D:\\cnblogtest\\login_user_error.jpg")

    def test_login_user_null(self):
        '''鐢ㄦ埛鍚嶄负绌恒�佸瘑鐮佹纭�'''
        self.login('', '!qaz2wsx')  # 鐢ㄦ埛鍚嶄负绌猴紝瀵嗙爜姝g‘
        error_message = self.dr.find_element_by_id('LoginName-error').text
        self.assertEqual(error_message, '璇疯緭鍏ョ櫥褰曠敤鎴峰悕')  # 鐢╝ssertEqual(a,b)鏂规硶鏉ユ柇瑷�  a == b
        self.dr.get_screenshot_as_file("D:\\cnblogtest\\login_user_null.jpg")

    def tearDown(self):
        sleep(2)
        print('鑷姩娴嬭瘯瀹屾瘯锛�')
        self.dr.quit()


if __name__ == '__main__':
    unittest.main()

閫氳繃浠ヤ笂浠g爜锛屾垜浠彲浠ョ湅鍒颁娇鐢╯elenium鍜寀nittest妗嗘灦缁撳悎锛屽凡缁忓彲浠ュ鐢ㄦ埛鐧婚檰鐨勬ā鍧楀仛涓�涓畝鍗曠殑鑷姩鍖栨祴璇曘��

浣嗘槸鐜板湪鍗氬鍥殑鐧婚檰鏈哄埗鐨勬敼鍙樻湁鍙兘閮ㄥ垎鐢ㄤ緥涓嶆垚鍔燂紝浣嗘槸浣犵煡閬撴�庝箞鍋氬氨鍙互浜嗐��

娴嬭瘯鎴浘濡備笅锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第5张图片

 姝g‘鐢ㄦ埛鍚嶅拰瀵嗙爜鐧诲綍鎴愬姛锛�

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第6张图片

 鐢ㄦ埛鍚嶆纭紝瀵嗙爜涓虹┖

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第7张图片

 鐢ㄦ埛鍚嶄负绌猴紝瀵嗙爜姝g‘

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第8张图片

5. 灏忕粨

 濂戒簡锛屼粖澶╃殑鍒嗕韩灏卞埌杩欓噷鍚э紒锛侊紒璋㈣阿鍚勪綅鐨勮�愬績闃呰銆�

 

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

 

銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�_第9张图片

你可能感兴趣的:(銆婃墜鎶婃墜鏁欎綘銆嬬郴鍒楃粌涔犵瘒涔�9-python+ selenium鑷姩鍖栨祴璇� -鐣绡� - 鏈�鍚庝竴娉㈠晩锛侊紒锛侊紙璇︾粏鏁欑▼锛�)