Python自動化xpath實現自動搶票搶貨
小伙伴們,這次推文講的是‘xpath‘,掌握起來不難的哦。而且,熟悉了這套路,別說pubmed,任何你能在瀏覽器實現的操作,都基本能通過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(’跳轉第’+str(i+1)+’頁面中’)driver.quit()print(’全部下載完畢,自動退出。’)
代碼1
for i in range(1,50+1): printg(’我錯了’)
操作如下:鼠標移動到Save按鈕→鼠標右擊→檢查,
可以發現源代碼有一塊區域亮起來了
這塊區域就是Save按鈕對應的源代碼→鼠標移動到該源代碼區域→右擊→復制→復制XPath,
這樣我們就復制了Save按鈕的Xpath了,接下來只需要粘貼到代碼就行.
小伙伴們是否還記得ActionChains的萬能公式:
ActionChains(driver).move_to_element(driver.find_element_by_xpath(’xpath_content’)).click().perform()
我們只需要把剛才復制的xpath粘貼到代碼中的“xpath_content”區域就行
是不是很熟悉呢,是的,這便是我們使用ActionChains模塊實現的第一個操作,后續的一樣噢,只需要復制對應的Xpath直接粘貼就行了。
最后,因為以上均是循環體的內容,當代碼執行完整個循環體了,我們可以通過代碼直接關掉瀏覽器,提醒我們打印完了,代碼如下
driver.quit()print(’全部下載完畢,結束了’)
因為這兩句代碼已經不是循環體的內容了,所以無需另起一行空兩個,而且,小伙伴記得噢,所有字符都是英文字符,包括括號、引號等等。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。
相關文章: