最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python自動(dòng)化實(shí)現(xiàn)的簡(jiǎn)單使用

 更新時(shí)間:2022年06月23日 09:13:53   作者:.弗蘭克  
本文主要介紹了python自動(dòng)化實(shí)現(xiàn)的簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

環(huán)境配置

  • pycharm環(huán)境配置
  • 下載chrome drives.exe,版本選擇要和chrome瀏覽器版本相近放在python目錄script
  • 在pycharm輸入以下代碼使python配合seninum實(shí)現(xiàn)自動(dòng)化輸入

定位

1.Link_text定位超鏈接

from selenium import webdriver

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")


#根據(jù)linktext方法定位超鏈接屬性界面字樣"新聞",看是否打開(kāi)新聞超鏈接(注意link_text值用單引號(hào))
#driver.find_element_by_link_text('新聞').click()

#根據(jù)partial_linktext方法定位超鏈接屬性界面模糊字樣"地",看是否打開(kāi)地圖超鏈接
driver.find_element_by_partial_link_text('地').click()

import time
# 沉睡的目的是讓程序進(jìn)行地慢一點(diǎn)方便觀看,有時(shí)候是等待元素渲染完成
time.sleep(5)

#關(guān)閉瀏覽器
#driver.quit()

2.混合元素定位

a.屬性定位方式

#查找所有id=root的元素
driver.find_element_by_xpath('//*[@id="root"]  

b.查找所有id值包含"con"的所有元素

注:contains()是個(gè)函數(shù)
//*[contains(@id,"con")]

c.查找所有包含id=footerLayer 且 包含class=relativeFooterLayer的所有元素

//*[@id="footerLayer" and @class="relativeFooterLayer" ]

d. 查找所有包含id=footerLayer 或者 包含class=relativeFooterLayer的所有元素

/*[@id="footerLayer" or @class="relativeFooterLayer" ]

f.查找 class以mod開(kāi)頭的div屬性

//div[starts-with(@class,'mod')]

3.Xpath定位(通常)

1.絕對(duì)定位

從頂層一層一層往下找對(duì)應(yīng)的元素
缺點(diǎn):頁(yè)面改動(dòng),絕對(duì)地址就會(huì)發(fā)生改變,將會(huì)定位錯(cuò)誤

from selenium import webdriver
import time

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
driver.get("https://www.ctrip.com/")

#使用xpath方法絕對(duì)定位點(diǎn)擊攜程的攻略.景點(diǎn)功能
driver.find_element_by_xpath('/html/body/div/div[@id="leftSideNavLayer"]/div/div/div[@style]/div/div/div/div[6]').click()
#沉睡的目的是讓程序進(jìn)行地慢一點(diǎn)方便觀看,有時(shí)候是等待元素渲染完成
sleep(2)

#關(guān)閉瀏覽器
driver.quit()

相對(duì)定位(經(jīng)常)

from selenium import webdriver
import time

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
driver.get("https://www.ctrip.com/")

#使用xpath方法模糊定位點(diǎn)擊攜程的攻略.景點(diǎn)功能
driver.find_element_by_xpath('//body//div[@id="root"]//div[@class="lsn_first_nav_wrap_LZamG"][5]').click()


#沉睡的目的是讓程序進(jìn)行地慢一點(diǎn)方便觀看,有時(shí)候是等待元素渲染完成
sleep(2)

#關(guān)閉瀏覽器
driver.quit()

4.css定位

注意:

1.空格是跨越一級(jí)或多級(jí)元素

2.>表示下層子標(biāo)簽

3.屬性:nth-child(1) ,表示相同屬性第一個(gè)

#表示id

查找所有id=ie-update的元素
#ie-update

.表示class

查找所有class=lsn_first_nav_wrap_LZamG的元素
.lsn_first_nav_wrap_LZamG

事例

為了定位小米note 11T pro+的價(jià)格

#表示定位class值是brick-list clearfix的ul標(biāo)簽之下的li標(biāo)簽,跨越多個(gè)層級(jí)下class名為price的p標(biāo)簽,下的span標(biāo)簽
ul[class="brick-list clearfix"]>li p.price>span

操作

1.實(shí)現(xiàn)輸入框自動(dòng)輸入

#此操作會(huì)自動(dòng)打開(kāi)瀏覽器,百度網(wǎng)址,輸入"你還好嗎","你怎么樣了","你今天過(guò)的怎么樣,充實(shí)嗎?"其中之一,而且加載時(shí)間會(huì)變慢五秒,完成以上結(jié)果后,會(huì)自動(dòng)關(guān)閉頁(yè)面

from selenium import webdriver

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")

#根據(jù)id屬性定位元素
#driver.find_element_by_id('kw').send_keys("你還好嗎")

#根據(jù)class屬性定位元素
#driver.find_element_by_class_name('s_ipt').send_keys("你怎么樣了")

#根據(jù)name屬性定位元素
driver.find_element_by_name('wd').send_keys("你今天過(guò)的怎么樣,充實(shí)嗎?")

import time
#沉睡的目的是讓程序進(jìn)行地慢一點(diǎn)方便觀看,有時(shí)候是等待元素渲染完成
time.sleep(5)

#關(guān)閉瀏覽器
driver.quit()

2.清空輸入框

from selenium import webdriver
import time

#創(chuàng)建瀏覽器,打開(kāi)百度輸入框
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")

#定義變量th為輸入框
th=driver.find_element_by_id('kw')

#在輸入框中輸入文字,清空,再輸入
time.sleep(3)
th.send_keys("說(shuō)不完的話.....")
time.sleep(1)
th.clear()
th.send_keys("你還好嗎")

time.sleep(2)

#關(guān)閉瀏覽器
driver.quit()

3.上傳文件

百度自動(dòng)上傳圖片搜索

from selenium import webdriver
import time

#創(chuàng)建瀏覽器
driver=webdriver.Chrome()
#瀏覽器輸入百度網(wǎng)址
driver.get("https://www.baidu.com/")

#點(diǎn)擊上傳按鈕
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[5]/div/div/form/span[1]/span[1]').click()
#點(diǎn)擊選擇文件
driver.find_element_by_xpath('/html/body/div[1]/div[1]/div[5]/div/div/form/div/div[2]/div[2]/input').send_keys('C:\\Users\\hhh\\Desktop\\百度元素.png')

time.sleep(6)
driver.quit()

4.自動(dòng)化執(zhí)行javaScript方法

在f12下console下輸入以下代碼

#因?yàn)槿掌诿吭碌拿刻煳恢枚疾灰粯?所以引入了document.querySelector()方法
#來(lái)查找class名為td的標(biāo)簽中的id值是"June-29-2022"
#所以若想點(diǎn)擊其他日期,直接改變id值就行

document.querySelector('[class="pi-input J_DepDate has-trans trigger-node-406"]').placeholder="2022-3-15"

在python下執(zhí)行javaScript格式

driver.execute_script('document.querySelector(\'[class="pi-input J_DepDate has-trans trigger-node-406"]\').placeholder="2022-3-15"')

5.瀏覽器窗口切換

from selenium import webdriver
import time

#創(chuàng)建瀏覽器,打開(kāi)百度輸入框
driver = webdriver.Chrome()
driver.get("https://www.baidu.com/")

#在輸入框中搜索
driver.find_element_by_id('kw').send_keys("hello,world")
time.sleep(2)

#點(diǎn)擊搜索
driver.find_element_by_id('su').click()

#點(diǎn)擊文本的百度百科
driver.find_element_by_xpath('//div[@class="bk_polysemy_1Ef6j"]//a').click()

time.sleep(5)

 #獲取所有的瀏覽器窗口
 windows=driver.window_handles

 #選中第二個(gè)新開(kāi)的瀏覽器窗口
 driver.switch_to.window(windows[-1])

#切換到第一個(gè)瀏覽器窗口
driver.switch_to.window(windows[0])

6.切換ifame

為什么要切換ifame

原因是網(wǎng)頁(yè)嵌套一個(gè)網(wǎng)頁(yè),不切換ifame,定位不到元素

from selenium import webdriver
import time

#創(chuàng)建瀏覽器,打開(kāi)阿里云注冊(cè)界面
driver = webdriver.Chrome()
driver.get("https://account.aliyun.com/register/qr_register.htm")

#定義一個(gè)變量frame  ,值是定位frame元素
frame = driver.find_element_by_id('alibaba-register-box')

#切到html下frame頁(yè)面
driver.switch_to.frame(frame)


#在注冊(cè)界面賬號(hào)欄輸入賬號(hào)
driver.find_element_by_xpath('//div[@class=" next-form-item-control"]//input').send_keys("17655870668")

#在注冊(cè)界面賬號(hào)欄輸入密碼
driver.find_element_by_xpath('//div[@class="pwd-wrap"] //input[@type="password"]').send_keys("edwdaf")

#再次切換到frame外層
driver.switch_to.parent_frame()

time.sleep(3)
driver.quit()

7.alert窗口切換

from selenium import webdriver
import time

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
#打開(kāi)我的本地彈窗html文件
driver.get("E:\\python_pro\\venv\\1.html")

#定義一個(gè)變量用于彈出的alert窗口
alert = driver.switch_to.alert

print(alert.text)

time.sleep(3)
driver.quit()

8.模擬鼠標(biāo)經(jīng)過(guò)操作

from selenium import webdriver
import time
#引入 ActionChains類(lèi)
from selenium.webdriver.common.action_chains import ActionChains



#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
#打開(kāi)京東界面
driver.get("https://www.jd.com")
time.sleep(3)

#定義一個(gè)變量m用于接收"網(wǎng)站導(dǎo)航"定位
m = driver.find_element_by_xpath('//*[@aria-label="網(wǎng)站導(dǎo)航"]')
#傳入變量m,并使鼠標(biāo)懸停在"網(wǎng)站導(dǎo)航"
ActionChains(driver).move_to_element(m).perform()#perform()方法代表執(zhí)行的意思

time.sleep(3)

#定義一個(gè)變量x用于接收"京東通信"定位
x = driver.find_element_by_xpath('//a[@role="menuitem" and text()="京東通信"]')
#傳入變量x,并使鼠標(biāo)點(diǎn)擊懸停在"網(wǎng)站導(dǎo)航"的"京東通信"子頁(yè)面
ActionChains(driver).move_to_element(x).click().perform()

time.sleep(3)
driver.quit()


9.模擬拖拽操作?

from selenium import webdriver
import time
#引入 ActionChains類(lèi)
from selenium.webdriver.common.action_chains import ActionChains



#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
action= ActionChains(driver)

#打開(kāi)地圖界面
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")
time.sleep(6)

time.sleep(3)
#定位地圖的定位
u=driver.find_element_by_xpath('//*[@type="system"]')
#  move_to_element(mask)移動(dòng)到這個(gè)元素,click_and_hold()按下鼠標(biāo),move_by_offset(50,30)偏移量拖動(dòng),release()松開(kāi)鼠標(biāo),perform()執(zhí)行操作
action.move_to_element(u).click_and_hold().move_by_offset(50,30).release().perform()


time.sleep(3)
driver.quit()

10.隱式等待?

為了解決一段時(shí)間頁(yè)面彈出登錄窗口

from selenium import webdriver
import time
#引入 ActionChains類(lèi)
from selenium.webdriver.common.action_chains import ActionChains

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()

#隱式等待頁(yè)面元素時(shí)間(0-15s)
driver.implicitly_wait(15)
#打開(kāi)地圖界面
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")



#記錄開(kāi)始時(shí)間
start_time = time.perf_counter()

#點(diǎn)擊賬號(hào)輸入
driver.find_element_by_xpath('//*[@name="userName"]').send_keys('1321454654')
#點(diǎn)擊密碼輸入
driver.find_element_by_xpath('//*[@name="password"]').send_keys('zawwe343234')
#點(diǎn)擊登錄
driver.find_element_by_xpath('//*[@value="登錄"]').click()


action= ActionChains(driver)

#定位地圖的定位
u=driver.find_element_by_xpath('//*[@type="system"]')
#  move_to_element(mask)移動(dòng)到這個(gè)元素,click_and_hold()按下鼠標(biāo),move_by_offset(50,30)偏移量拖動(dòng),release()松開(kāi)鼠標(biāo),perform()執(zhí)行操作
action.move_to_element(u).click_and_hold().move_by_offset(50,30).release().perform()


time.sleep(3)
driver.quit()

11.顯示等待?

可以控制單個(gè)元素等待時(shí)間

from selenium import webdriver
import time
#引入顯示等待函數(shù)
import selenium.webdriver.support.expected_conditions as EC
from selenium.webdriver.common.by import By


#創(chuàng)建瀏覽器
driver = webdriver.Chrome()
#打開(kāi)地圖界面
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")

#記錄開(kāi)始時(shí)間
#start_time = time.perf_counter()

WebDriverWait(driver,5).until(EC.presence_of_element_located(By.CSS_SELECTOR,'//*[@name="userName"]'))

#記錄結(jié)束時(shí)間
#end_time = time.perf_counter()
#print(f'{end_time - start_time} s')

#點(diǎn)擊賬號(hào)輸入
driver.find_element_by_xpath('//*[@name="userName"]').send_keys('345761231')
#點(diǎn)擊密碼輸入
driver.find_element_by_xpath('//*[@type="password" and @name="password"]').send_keys('21323dwe')


# time.sleep(3)
# driver.quit()

測(cè)試

1.自動(dòng)填寫(xiě)登錄csdn

from selenium import webdriver
import time

#創(chuàng)建瀏覽器
driver=webdriver.Chrome()

#瀏覽器輸入csdn網(wǎng)址
driver.get("https://www.csdn.net/?spm=1000.2115.3001.4476")
#找到超鏈接文本登錄/注冊(cè),然后點(diǎn)擊
driver.find_element_by_link_text('登錄/注冊(cè)').click()
#點(diǎn)擊登錄方式-密碼登錄
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div[1]/div[1]/span[4]').click()

#輸入csdn賬號(hào)
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[1]/div/input').send_keys('賬號(hào)')
#輸入csdn密碼
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[2]/div/input').send_keys('密碼')
#登錄
driver.find_element_by_xpath('/html/body/div[2]/div/div[2]/div[2]/div[1]/div/div[2]/div/div[4]/button').click()

time.sleep(10)
driver.quit()

2.隱式登錄百度地圖?

from selenium import webdriver
import time
#引入 ActionChains類(lèi)
from selenium.webdriver.common.action_chains import ActionChains

#創(chuàng)建瀏覽器
driver = webdriver.Chrome()

#隱式等待頁(yè)面元素時(shí)間(0-15s)
driver.implicitly_wait(5)
#打開(kāi)地圖界面
driver.get("https://map.baidu.com/@11590057.96,4489812.75,4z")



#記錄開(kāi)始時(shí)間
#start_time = time.perf_counter()

#點(diǎn)擊賬號(hào)輸入
driver.find_element_by_xpath('//*[@name="userName"]').send_keys('423425465')
#點(diǎn)擊密碼輸入
driver.find_element_by_xpath('//*[@type="password" and @name="password"]').send_keys('8vsdrw5')
# 點(diǎn)擊登錄
driver.find_element_by_xpath('//*[@value="登錄" and @type="submit"]').click()

#記錄結(jié)束時(shí)間
#end_time = time.perf_counter()
#print(f'{end_time - start_time} s')



action = ActionChains(driver)

#定位地圖的定位
u=driver.find_element_by_xpath('//*[@type="system"]')
#move_to_element(mask)移動(dòng)到這個(gè)元素,click_and_hold()按下鼠標(biāo),move_by_offset(50,30)偏移量拖動(dòng),release()松開(kāi)鼠標(biāo),perform()執(zhí)行操作
action.move_to_element(u).click_and_hold().move_by_offset(50,30).release().perform()


# time.sleep(3)
# driver.quit()

到此這篇關(guān)于python自動(dòng)化實(shí)現(xiàn)的簡(jiǎn)單使用的文章就介紹到這了,更多相關(guān)python自動(dòng)化 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python函數(shù)的萬(wàn)能參數(shù)傳參詳解

    python函數(shù)的萬(wàn)能參數(shù)傳參詳解

    這篇文章主要介紹了python函數(shù)的萬(wàn)能參數(shù)傳參詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • 基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識(shí)別

    基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識(shí)別

    這篇文章主要為大家詳細(xì)介紹了基于python神經(jīng)卷積網(wǎng)絡(luò)的人臉識(shí)別,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Python工程實(shí)踐之np.loadtxt()讀取數(shù)據(jù)

    Python工程實(shí)踐之np.loadtxt()讀取數(shù)據(jù)

    為了方便使用和記憶,有時(shí)候我們會(huì)把numpy.loadtxt()縮寫(xiě)成np.loadtxt(),這篇文章主要給大家介紹了關(guān)于Python工程實(shí)踐之np.loadtxt()讀取數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下
    2023-07-07
  • Python實(shí)現(xiàn)復(fù)制文檔數(shù)據(jù)

    Python實(shí)現(xiàn)復(fù)制文檔數(shù)據(jù)

    我們百度搜索一些東西得時(shí)候,經(jīng)常找到文檔里面然后就會(huì)發(fā)現(xiàn)需要充值才能復(fù)制!怎么可以不花錢(qián)也保存呢?今天就分享給大家一個(gè)python獲取文檔數(shù)據(jù)得方法,需要的可以收藏一下
    2022-12-12
  • Python 實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)截圖的示例講解

    Python 實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)截圖的示例講解

    今天小編就為大家分享一篇Python 實(shí)現(xiàn)網(wǎng)頁(yè)自動(dòng)截圖的示例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-05-05
  • Python如何使用帶有?for?循環(huán)的?Lambda?函數(shù)

    Python如何使用帶有?for?循環(huán)的?Lambda?函數(shù)

    這篇文章主要介紹了如何在?Python?中使用帶有?for?循環(huán)的?Lambda?函數(shù),使用?Lambda?函數(shù)配合?for?循環(huán)可以讓代碼變得更加簡(jiǎn)潔,但需要注意的是,Lambda?函數(shù)在語(yǔ)法上有一些限制,如果需要更復(fù)雜的邏輯,還需要使用普通函數(shù),感興趣的朋友跟隨小編一起學(xué)習(xí)吧
    2023-05-05
  • Python 中的函數(shù)裝飾器和閉包詳解

    Python 中的函數(shù)裝飾器和閉包詳解

    這篇文章主要介紹了Python 中的函數(shù)裝飾器和閉包詳解,需要的朋友可以參考下
    2021-02-02
  • Python基于pycrypto實(shí)現(xiàn)的AES加密和解密算法示例

    Python基于pycrypto實(shí)現(xiàn)的AES加密和解密算法示例

    這篇文章主要介紹了Python基于pycrypto實(shí)現(xiàn)的AES加密和解密算法,結(jié)合實(shí)例形式分析了Python使用pycrypto模塊進(jìn)行AES加密與解密操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2018-04-04
  • 詳解python架構(gòu)?PyNeuraLogic超越Transformers

    詳解python架構(gòu)?PyNeuraLogic超越Transformers

    這篇文章主要為大家介紹了python使用?PyNeuraLogic超越Transformers示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • Python3.9.1中使用match方法詳解

    Python3.9.1中使用match方法詳解

    這篇文章主要介紹了Python3.9.1中使用match方法詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02

最新評(píng)論

浦江县| 固始县| 江华| 左云县| 鄂伦春自治旗| 齐河县| 中牟县| 福建省| 宁河县| 揭阳市| 丹东市| 济源市| 宕昌县| 宁海县| 新和县| 陇西县| 太白县| 岐山县| 彭山县| 四会市| 五峰| 古蔺县| 新河县| 石家庄市| 仪征市| 龙里县| 涟水县| 开江县| 温泉县| 平原县| 都匀市| 克什克腾旗| 定陶县| 梧州市| 宁强县| 天台县| 监利县| 祁阳县| 泸州市| 江北区| 比如县|