selenium实战学习--定位元素
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实战学习--定位元素的更多相关文章
- Selenium webdriver 学习总结-元素定位
Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...
- selenium+iframe 如何定位元素(实战)
场景: 在同一界面,需定位iframe里面的元素, 就需要切换至Iframe块,然后定位元素,验证完成后,再切换出来. 如果不切换至iframe ,会发现不管采取什么定位,都会报元素不存在.
- selenium的八大定位元素的方式
#八大定位方式 from selenium import webdriver def BrowserOpen(): driver = webdriver.Chrome(); driver.maximi ...
- selenium使用location定位元素坐标偏差
python+selenium+Chromedriver使用location定位元素坐标偏差使用xpath定位元素,用.location获取坐标值,截取网页截图的一部分出现偏差. 之所以会出现这个坐标 ...
- python + selenium 练习篇 - 定位元素的方法
1.利用ID定位元素(能直接通过ID来定位的元素比较少) # coding=utf-8from selenium import webdriverdriver = webdriver.Chrome() ...
- 【Selenium01篇】python+selenium实现Web自动化:搭建环境,Selenium原理,定位元素以及浏览器常规操作!
一.前言 最近问我自动化的人确实有点多,个人突发奇想:想从0开始讲解python+selenium实现Web自动化测试,请关注博客持续更新! 二.话不多说,直接开干,开始搭建自动化测试环境 这里以前在 ...
- selenium八种定位元素方法
1.driver.find_element_by_id('su') 定位到元素的id一般id是唯一的,可以精确定位到元素 2.driver.find_element_by_name() 通过元素的na ...
- selenium(3):学习操作元素基本方法
来源:<selenium webdriver基于Python源码案例> 一.打开网页①第一步:从selenium里面导入webdriver模块②打开浏览器(Ie和Chrome对应下面的)③ ...
- selenium实战学习第一课
#-*- coding:utf-8 -*- __author__ = "carry" from selenium import webdriver from selenium.we ...
- Selenium(三)webdriver的API与定位元素
在学习定位元素之前,应该要学会: 1.打开浏览器 2.打开网页 3.定位元素及操作 ①定位元素 可只此输入框的id是kw,name是wd,class是s_ipt ②在python编辑器中找到该元素 通 ...
随机推荐
- python数据类型、变量以及编码和字符串、格式化
1.数据类型包括整型.浮点型.字符串.布尔型. 整数如果位数太多可以用_隔开,浮点数可以用科学记数法表示,字符串要用单引号或者双引号括起来,布尔型的值只能为True和False 2.变量可以由数字.字 ...
- 【Diary】CSP-S 2020 游记
一年 好快 从三百多天倒计时 一点一点掂着 又回来了 但是时间永远不会等待你. --??? CSP-J1/S1 CSP-J1/S1 Day0 请了一上午假. 这段时间都在摸鱼,作业没写( 多备赛一个上 ...
- Java并发(二)----初次使用多线程并行提高效率
1.并行 并行代表充分利用多核 cpu 的优势,提高运行效率. 想象下面的场景,执行 3 个计算,最后将计算结果汇总. 计算 1 花费 10 ms 计算 2 花费 11 ms 计算 3 花费 ...
- defineProperty在数据劫持后是如何通知数据的更新和视图的更新的
vue的双向绑定是由数据劫持结合发布者-订阅者模式实现的,那么什么是数据劫持?vue是如何进行数据劫持的?说白了就是通过Object.defineProperty()来劫持对象属性的setter和ge ...
- 25-tree shaking(树摇)
const { resolve } = require('path'); const MiniCssExtractPlugin = require('mini-css-extract-plugin') ...
- TypeScript 学习笔记 — 数组常见的类型转换操作记录(十四)
获取长度 length type LengthOfTuple<T extends any[]> = T["length"]; type A = LengthOfTupl ...
- 考前必备fa宝——对拍
2022.11.24:晚上zxs学长发来了他的博客,所以我仿照写一篇. https://www.cnblogs.com/Dita/p/duipai.html 对拍 对拍这个东西,就是可以比较两份代码跑 ...
- ai问答:vue3+pinia+WebSocket 封装断线重连(实战)
把socket实例 挂载到全局 为方便梳理,请忽略typescript # main.ts import {createApp} from 'vue' import App from './App.v ...
- SQL语句获取数据表结构定义,适合导出EXCEL为文档
SELECT 表名 = Case When A.colorder=1 Then D.name Else '' End, 表说明 = Case When A.colorder=1 Then isnull ...
- Hibernate 基本操作、懒加载以及缓存
前言 上一篇咱们介绍了 Hibernate 以及写了一个 Hibernate 的工具类,快速入门体验了一波 Hibernate 的使用,我们只需通过 Session 对象就能实现数据库的操作了. 现在 ...