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. [数据库]MySQL解决:MySQLNonTransientConnectionException: Could not create connection to database server.【待完善】

    场景复现 mysql数据库 5.7.24 jdbc driver: mysql-connector-java: 5.1.33 jdbc 配置: + jdbc.url + driverName: Tom ...

  2. Java设计模式 —— 装饰模式

    12 装饰模式 12.1 装饰模式概述 Decorator Pattern: 动态地给一个对象增加一些额外的职责.提供一种比使用子类更加灵活的方案来扩展功能. 装饰模式是一种用于替代继承的技术,通过一 ...

  3. HTML、 input;、accept 属性-规定能够通过文件上传进行提交的文件类型

    定义和用法 文章地址: http://www.w3school.com.cn/tags/att_input_accept.asp accept 属性规定了可通过文件上传提交的服务器接受的文件类型. 注 ...

  4. linux网络开发者定位问题常用工具和命令总结

    本文章来自我的微信个人技术公众号---网络技术修炼,公众号中总结普及网络基础知识,包括基础原理.网络方案.开发经验和问题定位案例等,欢迎关注. Linux网络开发者面临的问题往往比较复杂,因此需要使用 ...

  5. Gin框架快速入门

    github地址: https://github.com/gin-gonic/gin 初体验 安装: $ go get -u github.com/gin-gonic/gin 简单实例: packag ...

  6. 14-压缩css

    const { resolve } = require('path') const HtmlWebpackPlugin = require('html-webpack-plugin') const M ...

  7. jdbc-plus是一款基于JdbcTemplate增强工具包, 基于JdbcTemplate已实现分页、多租户等插件,可自定义扩展插件

    jdbc-plus简介 jdbc-plus是一款基于JdbcTemplate增强工具包, 基于JdbcTemplate已实现分页.多租户等插件,可自定义扩展插件.项目地址: https://githu ...

  8. sqlmap安全测试工具使用简介

    SQLmap是一个自动化的SQL注入工具,其主要功能是扫描,发现并利用给定的URL的SQL注入漏洞,目前支持的数据库是MySQL,Oracle,PostgreSQL,Microsoft  SQL  S ...

  9. STL大全

    排序最速传说--sort 我们都学过一些排序的写法,比如冒泡排序,快速排序之类的东西,举个例子来说吧,这是快速排序的代码: #include<iostream> using namespa ...

  10. MyBatis 配置详解

    目录 mybatis-config.xml 核心配置文件 1. environments元素 1.1 子元素environment 1.2 transactionManager 事务管理器(共两种) ...