from selenium import webdriver
from selenium.webdriver.common.by import By

from selenium.common import exceptions as ex
import lxml
import time

#获取到webdriver路径供调用
driver = webdriver.Chrome(r'C:\Users\DELL\AppData\Local\Google\Chrome\Application\chromedriver.exe')
#设置窗口大小
driver.set_window_size(1280,1000)

#设置全局操作超时时间
driver.implicitly_wait(10)
#通过webdriver访问网站
driver.get('https://kyfw.12306.cn/otn/resources/login.html')

driver.find_element(By.LINK_TEXT, "账号登录").click()
driver.find_element(By.ID, "J-userName").click()
driver.find_element(By.ID, "J-userName").send_keys("*********") #登录名自己用的填充
driver.find_element(By.ID, "J-password").click()
driver.find_element(By.ID, "J-password").send_keys("*******") #登录密码自己用的填充明文
time.sleep(10)#等待30s,利用空隙手动输入验证码

#立即登录按钮有覆盖层,使用enter触发,ljdl.send_keys("\n")实现enter键触发
ljdl = driver.find_element(By.ID, "J-login")
ljdl.send_keys("\n")
print("1")
time.sleep(3)

driver.find_element_by_link_text("车票").click()
driver.find_element(By.LINK_TEXT, "单程").click()

#对StaleElementReferenceException异常的处理
try:
driver.find_element_by_link_text("车票").click()
driver.find_element(By.LINK_TEXT, "单程").click()
except ex.StaleElementReferenceException:
driver.find_element_by_link_text("车票").click()
driver.find_element(By.LINK_TEXT, "单程").click()
#from nanchang
time.sleep(3)
driver.find_element(By.ID, "fromStationText").click()
driver.find_element_by_css_selector( u"[title=ww昌]").click()

# to wuhan
time.sleep(5)
driver.find_element(By.ID, "toStationText").click()
driver.find_element_by_css_selector( u"[title=ee汉]").click()

#date at 18
time.sleep(5)
driver.find_element(By.ID, "train_date").click()
#获取乘车日期的时候,选中日期右键“检查”,进入li 列表 copy selector
driver.find_element_by_css_selector( "body > div.cal-wrap >"
" div.cal.cal-right > div.cal-cm > div:nth-child(16) > div").click()

# choose train type

time.sleep(5)
driver.find_element_by_css_selector("#_ul_station_train_code > li:nth-child(3) > label").click()

time.sleep(5)

while True:
try:
driver.find_element_by_id("query_ticket").click() #click the button
e=driver.find_element_by_id("YZ_5n0000Z12601") #che de xinghao
e.click()
if e.text in [u'无','--']:
print("no Tickets")
time.sleep(2)
else:
print("yes")
driver.find_element_by_link_text("预定").click()
#选择乘车人,列表的第一个和第2个
driver.find_element_by_css_selector("#normal_passenger_id > li:nth-child(1) > label").click()
driver.find_element_by_css_selector("#normal_passenger_id > li:nth-child(2) > label").click()
#提交订单以及确认提交付款
driver.find_element_by_link_text("提交订单").click()
driver.find_element_by_link_text("确认").click()

except:
pass
==============================================遇到的问题集锦====================================================
1、一直报错:unknown error: Element is not clickable at point (580, 47). Other element would receive the click: <label for="stb_1">...</label>
#立即登录按钮有覆盖层,使用enter触发,ljdl.send_keys("\n")实现enter键触发
ljdl = driver.find_element(By.ID, "J-login")
ljdl.send_keys("\n")

参考微博:https://www.cnblogs.com/shengs/p/11203221.html

2、报错:AttributeError: 'list' object has no attribute 'click'
driver.find_elements_by_css_selector("#_ul_station_train_code > li:nth-child(3) > label").click()

查询资料说find_elements_by_css_selector中elements会elements集合,解决办法很简单,那就是去掉s,取单个即可

3、脚本第一次跑正常,第二次报错
解决办法很暴力:关掉重新跑

4、关于find_element_by_css_selector
对于蒙层,就打开蒙层然后右键--检查。获取蒙层list表,copy selector来定位元素,很好用,没有唯一id的时候非常好用


参考别人的写出来,但是自己也在不断修改不断尝试,期待自己的进步,感谢各位大侠的博客,让自己遇到问题不茫然,自学不孤独
本文参考代码:https://blog.csdn.net/github_37216944/article/details/79053781
 

selenium实战学习--定位元素的更多相关文章

  1. Selenium webdriver 学习总结-元素定位

    Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...

  2. selenium+iframe 如何定位元素(实战)

    场景: 在同一界面,需定位iframe里面的元素, 就需要切换至Iframe块,然后定位元素,验证完成后,再切换出来. 如果不切换至iframe ,会发现不管采取什么定位,都会报元素不存在.

  3. selenium的八大定位元素的方式

    #八大定位方式 from selenium import webdriver def BrowserOpen(): driver = webdriver.Chrome(); driver.maximi ...

  4. selenium使用location定位元素坐标偏差

    python+selenium+Chromedriver使用location定位元素坐标偏差使用xpath定位元素,用.location获取坐标值,截取网页截图的一部分出现偏差. 之所以会出现这个坐标 ...

  5. python + selenium 练习篇 - 定位元素的方法

    1.利用ID定位元素(能直接通过ID来定位的元素比较少) # coding=utf-8from selenium import webdriverdriver = webdriver.Chrome() ...

  6. 【Selenium01篇】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!

    一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 二.话不多说,直接开干,开始搭建自动化测试环境 这里以前在 ...

  7. selenium八种定位元素方法

    1.driver.find_element_by_id('su') 定位到元素的id一般id是唯一的,可以精确定位到元素 2.driver.find_element_by_name() 通过元素的na ...

  8. selenium(3):学习操作元素基本方法

    来源:<selenium webdriver基于Python源码案例> 一.打开网页①第一步:从selenium里面导入webdriver模块②打开浏览器(Ie和Chrome对应下面的)③ ...

  9. selenium实战学习第一课

    #-*- coding:utf-8 -*- __author__ = "carry" from selenium import webdriver from selenium.we ...

  10. Selenium(三)webdriver的API与定位元素

    在学习定位元素之前,应该要学会: 1.打开浏览器 2.打开网页 3.定位元素及操作 ①定位元素 可只此输入框的id是kw,name是wd,class是s_ipt ②在python编辑器中找到该元素 通 ...

随机推荐

  1. 单元测试、文档测试、读写文件、StringIO和BytesIO

    1.单元测试就是为了测试程序执行的正确性 2.编写单元测试时,需要编写一个单元测试类,继承unittest TestCase类 3.单元测试最常用的断言是assert,断言期望抛出指定的异常 4.运行 ...

  2. 【SpringBoot2】 SpringBoot2核心技术 基础

    写在前面 1 SpringBoot2核心技术 基础 1.1 Spring与SpringBoot SpringBoot是一个高层框架 1.2 项目创建 1.2.1 创建POM ①导入spring-boo ...

  3. 【Spring5】AOP

    3 AOP 面向切面编程,利用AOP可以对业务的各个逻辑进行隔离,从而使得业务逻辑各部分的耦合度之间降低,提高程序的可重用性,同时提高开发的效率. 目的:不通过修改源代码,在主干功能上增加新功能 AO ...

  4. javasec(三)类加载机制

    这篇文章介绍java的类加载机制. Java是一个依赖于JVM(Java虚拟机)实现的跨平台的开发语言.Java程序在运行前需要先编译成class文件,Java类初始化的时候会调用java.lang. ...

  5. js与java对json的操作

    JSON呢,是现在大部分,并且主流的传递数据的方式. 今天讲一下javascript的java对json的操作 提到js,当下就有一个比较主流的插件,vue.js,这个插件程序员没用过也都听说过吧, ...

  6. 笔记:C++学习之旅---面向对象程序设计2

    笔记:C++学习之旅---面向对象程序设计2 面向对象程序设计基于三个基本概念:数据抽象.继承和动态绑定. 继承和动态绑定对程序的编写有两方面的影响:一是我们可以更容易的定义与其他类相似但不完全相同的 ...

  7. TensorFlow - 框架实现中的三种 Graph

    文章目录 TensorFlow - 框架实现中的三种 Graph 1. Graph 2. GraphDef 3. MetaGraph 4. Checkpoint 5. 总结 TensorFlow - ...

  8. ToF技术全解读

    文章目录 ToF技术全解读 1. 什么是ToF 2. ToF的原理 3. ToF 优缺点 ToF技术全解读 1. 什么是ToF ToF: Time of flight. 飞行时间.当然这只是一种翻译的 ...

  9. 是时候,升级你的 Windows 了「GitHub 热点速览」

    不知道多少小伙伴用着 Windows 操作系统,可能会有一个烦恼是有时候操作系统过慢,因为众多拖慢 Windows 系统的组件.Atlas 作为一个修改版的 Windows 系统,能极大提高操作系统运 ...

  10. JUC并发编程原理精讲(源码分析)

    1. JUC前言知识 JUC即 java.util.concurrent 涉及三个包: java.util.concurrent java.util.concurrent.atomic java.ut ...