Python自動(dòng)化xpath實(shí)現(xiàn)自動(dòng)搶票搶貨
小伙伴們,這次推文講的是‘xpath‘,掌握起來(lái)不難的哦。而且,熟悉了這套路,別說(shuō)pubmed,任何你能在瀏覽器實(shí)現(xiàn)的操作,都基本能通過(guò)selenium自動(dòng)化進(jìn)行。
總代碼:
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)+’頁(yè)下載成功’) print(’跳轉(zhuǎn)第’+str(i+1)+’頁(yè)面中’)driver.quit()print(’全部下載完畢,自動(dòng)退出。’)
代碼1
for i in range(1,50+1): printg(’我錯(cuò)了’)
操作如下:鼠標(biāo)移動(dòng)到Save按鈕→鼠標(biāo)右擊→檢查,
可以發(fā)現(xiàn)源代碼有一塊區(qū)域亮起來(lái)了
這塊區(qū)域就是Save按鈕對(duì)應(yīng)的源代碼→鼠標(biāo)移動(dòng)到該源代碼區(qū)域→右擊→復(fù)制→復(fù)制XPath,
這樣我們就復(fù)制了Save按鈕的Xpath了,接下來(lái)只需要粘貼到代碼就行.
小伙伴們是否還記得ActionChains的萬(wàn)能公式:
ActionChains(driver).move_to_element(driver.find_element_by_xpath(’xpath_content’)).click().perform()
我們只需要把剛才復(fù)制的xpath粘貼到代碼中的“xpath_content”區(qū)域就行
是不是很熟悉呢,是的,這便是我們使用ActionChains模塊實(shí)現(xiàn)的第一個(gè)操作,后續(xù)的一樣噢,只需要復(fù)制對(duì)應(yīng)的Xpath直接粘貼就行了。
最后,因?yàn)橐陨暇茄h(huán)體的內(nèi)容,當(dāng)代碼執(zhí)行完整個(gè)循環(huán)體了,我們可以通過(guò)代碼直接關(guān)掉瀏覽器,提醒我們打印完了,代碼如下
driver.quit()print(’全部下載完畢,結(jié)束了’)
因?yàn)檫@兩句代碼已經(jīng)不是循環(huán)體的內(nèi)容了,所以無(wú)需另起一行空兩個(gè),而且,小伙伴記得噢,所有字符都是英文字符,包括括號(hào)、引號(hào)等等。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. 父div高度不能自適應(yīng)子div高度的解決方案2. ASP.NET MVC解決上傳圖片臟數(shù)據(jù)的方法3. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)4. JSP狀態(tài)管理的簡(jiǎn)單介紹5. jsp+mysql實(shí)現(xiàn)網(wǎng)頁(yè)的分頁(yè)查詢6. servlet+jsp實(shí)現(xiàn)過(guò)濾器 防止用戶未登錄訪問(wèn)7. 選擇模式 - XSL教程 - 28. Java之JSP教程九大內(nèi)置對(duì)象詳解(中篇)9. 淺談XML Schema中的elementFormDefault屬性10. ASP中SELECT下拉菜單同時(shí)獲取VALUE和TEXT值的實(shí)現(xiàn)代碼
