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. 你知道Vue响应式数据原理吗

    1. Vue2的响应式式原理主要是通过Object.defineProperty的方法里面的setter和getter方法的观察者模式来实现.也就是在组件的初始话阶段给每一个data属性都注册一个se ...

  2. react中受控组件与非受控组件--

    非受控组件:随用随取 1 render() { 2 return ( 3 <div> 4 <h1>非受控组件</h1> 5 <form action=&quo ...

  3. 计算机网络之MAC和IP地址

    MAC地址 在局域网中,硬件地址,又称为物理地址或MAC地址. 目前现在的局域网中实际上使用的都是6字节的MAC地址,所以每一个以太网设备都具有唯一的MAC地址. MAC地址的格式 假设传输使用的是I ...

  4. Java常见的线程池的创建及使用

    文章目录 线程池是什么? 线程池的主要参数 线程池的拒绝策略 创建线程池的方式 关闭线程池 大家好,我是Leo!今天准备和大家一起分享的知识是线程池,刚好今天在看八股文,就顺带写一下并把一些实践的例子 ...

  5. java中的 \r——字符串消失了

    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) 使用Integer.parseInt( ...

  6. SqliLabs 第二关 ,数字型注入!!!

    首先打开网页,进行注入点的测试 输入?id=1 and 1=1发现1=2的时候出现了报错,说明服务器接收了我们的指令,并且进行了反馈,说明了有注入点的存在,然后对注入点进行测试 然后输入order b ...

  7. golang调用sdl2,播放pcm音频,报错signal arrived during external code execution。

    golang调用sdl2,播放pcm音频,报错signal arrived during external code execution. win10 x64下测试成功,其他操作系统下不保证成功. 采 ...

  8. Pytest - 概述&入门

    Pytest - 概述&入门 概述 简介 Pytest是一款强大的python自动化测试工具,可以胜任各种类型或者级别的软件测试工作: pytest提供了丰富的功能,包括assert重写,第三 ...

  9. 创建CMDB项目

  10. 没想到还有这种骚操作~如何使用Golang实现无头浏览器截图?

    前言 在Web开发中,有时需要对网页进行截图,以便进行页面预览.测试等操作. 而使用无头浏览器来实现截图功能,可以避免手动操作的繁琐和不稳定性. 这篇文章将介绍:使用Golang进行无头浏览器的截图, ...