Python自动化xpath实现自动抢票抢货

发布时间:2020-10-24 20:56:54 作者:松鼠爱吃饼干
来源:脚本之家 阅读:156

小伙伴们,这次推文讲的是‘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

Python自动化xpath实现自动抢票抢货

for i in range(1,50+1): printg('我错了')

Python自动化xpath实现自动抢票抢货

Python自动化xpath实现自动抢票抢货

操作如下:鼠标移动到Save按钮→鼠标右击→检查,

可以发现源代码有一块区域亮起来了

这块区域就是Save按钮对应的源代码→鼠标移动到该源代码区域→右击→复制→复制XPath,

这样我们就复制了Save按钮的Xpath了,接下来只需要粘贴到代码就行.

Python自动化xpath实现自动抢票抢货

Python自动化xpath实现自动抢票抢货

小伙伴们是否还记得ActionChains的万能公式:

ActionChains(driver).move_to_element(driver.find_element_by_xpath('xpath_content')).click().perform()

我们只需要把刚才复制的xpath粘贴到代码中的“xpath_content”区域就行

是不是很熟悉呢,是的,这便是我们使用ActionChains模块实现的第一个操作,后续的一样噢,只需要复制对应的Xpath直接粘贴就行了。

Python自动化xpath实现自动抢票抢货

最后,因为以上均是循环体的内容,当代码执行完整个循环体了,我们可以通过代码直接关掉浏览器,提醒我们打印完了,代码如下

driver.quit()
print('全部下载完毕,结束了')

因为这两句代码已经不是循环体的内容了,所以无需另起一行空两个,而且,小伙伴记得噢,所有字符都是英文字符,包括括号、引号等等。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。

推荐阅读:
  1. Python怎么实现12306火车票抢票系统
  2. 使用python+selenium实现自动抢票功能

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python xpath

上一篇:Python制作词云的方法

下一篇:java原生序列化和Kryo序列化性能实例对比分析

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》