Python自動化xpath實現(xiàn)自動搶票搶貨
小伙伴們,這次推文講的是‘xpath‘,掌握起來不難的哦。而且,熟悉了這套路,別說pubmed,任何你能在瀏覽器實現(xiàn)的操作,都基本能通過selenium自動化進行。
總代碼:
for i in range(51,56): driver.implicitly_wait(10) ActionChains(driver).move_to_element(driver.find_element_by_xpath(’//*[@id='save-results-panel-trigger']’)).click().perform() Select(driver.find_element_by_xpath(’//*[@id='save-action-selection']’)).select_by_visible_text('All results on this page') ActionChains(driver).move_to_element(driver.find_element_by_xpath(’//*[@id='save-action-format']’)).click().perform() Select(driver.find_element_by_xpath(’/html/body/main/div[1]/div/form/div[2]/select’)).select_by_visible_text('CSV') ActionChains(driver).move_to_element(driver.find_element_by_xpath(’//*[@id='save-action-panel-form']/div[3]/button[1]’)).click().perform() target =driver.find_element_by_xpath(’//*[@id='search-results']/section/div[3]/a/span’) driver.execute_script('arguments[0].();', target) ActionChains(driver).move_to_element(driver.find_element_by_xpath(’//*[@id='search-results']/section/div[3]/a/span’)).click().perform() ActionChains(driver).move_to_element(driver.find_element_by_xpath(’//*[@id='search-page']/div[12]/div/form/button’)).click().perform() print(’第’+str(i)+’頁下載成功’) print(’跳轉(zhuǎn)第’+str(i+1)+’頁面中’)driver.quit()print(’全部下載完畢,自動退出。’)
代碼1
for i in range(1,50+1): printg(’我錯了’)
操作如下:鼠標移動到Save按鈕→鼠標右擊→檢查,
可以發(fā)現(xiàn)源代碼有一塊區(qū)域亮起來了
這塊區(qū)域就是Save按鈕對應的源代碼→鼠標移動到該源代碼區(qū)域→右擊→復制→復制XPath,
這樣我們就復制了Save按鈕的Xpath了,接下來只需要粘貼到代碼就行.
小伙伴們是否還記得ActionChains的萬能公式:
ActionChains(driver).move_to_element(driver.find_element_by_xpath(’xpath_content’)).click().perform()
我們只需要把剛才復制的xpath粘貼到代碼中的“xpath_content”區(qū)域就行
是不是很熟悉呢,是的,這便是我們使用ActionChains模塊實現(xiàn)的第一個操作,后續(xù)的一樣噢,只需要復制對應的Xpath直接粘貼就行了。
最后,因為以上均是循環(huán)體的內(nèi)容,當代碼執(zhí)行完整個循環(huán)體了,我們可以通過代碼直接關(guān)掉瀏覽器,提醒我們打印完了,代碼如下
driver.quit()print(’全部下載完畢,結(jié)束了’)
因為這兩句代碼已經(jīng)不是循環(huán)體的內(nèi)容了,所以無需另起一行空兩個,而且,小伙伴記得噢,所有字符都是英文字符,包括括號、引號等等。
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Warning: require(): open_basedir restriction in effect,目錄配置open_basedir報錯問題分析2. ASP.NET Core 5.0中的Host.CreateDefaultBuilder執(zhí)行過程解析3. ASP調(diào)用WebService轉(zhuǎn)化成JSON數(shù)據(jù),附json.min.asp4. SharePoint Server 2019新特性介紹5. ASP中常用的22個FSO文件操作函數(shù)整理6. React+umi+typeScript創(chuàng)建項目的過程7. php網(wǎng)絡安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究8. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁9. 三個不常見的 HTML5 實用新特性簡介10. php測試程序運行速度和頁面執(zhí)行速度的代碼
