首页 > 编程语言 >python_selenium元素定位_xpath(2)

python_selenium元素定位_xpath(2)

时间:2023-01-06 16:13:43浏览次数:39  
标签:xpath python selenium driver element click div find

 


selenium自动化脚本最基础的就是元素定位和元素操作,下面就以百度为例介绍最常见的xpath定位方式

基本定位方式:

以百度的搜索框为例


from selenium import webdriver
import time
driver = webdriver.Chrome()
driver.get("https://www.baidu.com")
driver.maximize_window()
time.sleep(2)
# 1、绝对路径
# driver.find_element_by_xpath("/html/body/div/div/div/div/div/form/span/input").send_keys("龙猫")
# 2、相对路径
# driver.find_element_by_xpath("//form/span/input").send_keys("龙猫")
# 3、通过元素索引定位
# driver.find_element_by_xpath("//div/div[3]/a[3]").click()
# 4、使用元素属性定位
# 4.1 单属性
# driver.find_element_by_xpath("//input[@maxlength = '255']").send_keys("小狗")
# 4.2 多属性and
# driver.find_element_by_xpath("//input[@maxlength='255' and @autocomplete='off']").send_keys("小狗")
# 4.3 多属性or
# driver.find_element_by_xpath("//input[@maxlength='259' or @autocomplete='off']").send_keys("小狗")
# 5、模糊匹配
# 5.1 以什么开头starts-with()
# driver.find_element_by_xpath("//a[starts-with(@name,'tj_trn')]").click()
# 5.2 以什么结尾substring()
# driver.find_element_by_xpath("//a[substring(@name,6)='news']").click()
# 5.3 包含contains()
# driver.find_element_by_xpath("//a[contains(@name,'trne')]").click()
# 6、使用元素文本定位text()函数
# driver.find_element_by_xpath("//a[text()='新闻']").click()
driver.find_element_by_xpath("//a[contains(text(),'新')]").click()

这些就是xpath定位最常用的,至于怎么选择使用就看自己具体的使用情况了。

标签:xpath,python,selenium,driver,element,click,div,find
From: https://www.cnblogs.com/xmxit/p/17030756.html

相关文章

  • python + selenium 常用公共方法封装
     selenium环境配置及浏览器驱动的安装:https://www.cnblogs.com/gancuimian/p/16435300.htmluiautomator2常用公共方法封装见之前的帖子:https://www.cnblogs.com/gancu......
  • Python爬取往期股票数据,分析中奖规律!
    快过年了,手头有点紧,但是作为一个男人,身上怎么能够没有大把钞票呢?于是我决定用Python来分析一波股票,赢了会所嫩*,输了下海干活!好了,上面是我吹牛逼的,不过确实有小伙......
  • python 调试 qml
    1.设置pycharm的parameters-qmljsdebugger=port:10002,block 2.python启动调试:点击debug按钮   3.设置qtcreaterqtcreater中打开要调试的文件,打上断点......
  • 基于Python的K-Means遥感影像聚类
    importnumpyasnpfromsklearnimportclusterfromosgeoimportgdal,gdal_arrayimportmatplotlib.pyplotaspltgdal.UseExceptions()gdal.AllRegister()img......
  • 用Python实现一个基于文件存储的控制台学生管理系统
    放假回家,写写Python玩,10分钟写了一个基于文件存储的控制台学生管理系统,目的是熟悉Json的使用importjsonimportosimportatexitimporttimelatest_file=""defm......
  • Python 迭代器Iterator详情
    1.什么是迭代器?迭代器是一个表示数据流的对象,当我们调用next()方法时会返回容器中的下一个值迭代器中包含__iter__和__next__()方法。通过__iter__方法可以返回迭代器......
  • 推送自研包到python仓库
    环境Python版本:3.6.8仓库:JFrogArtifactory步骤包制作https://zhuanlan.zhihu.com/p/37987613.pypirc文件在$home目录下创建.pypirc文件从Artifactory平......
  • 聊天尬住了?教你用Python一键获取斗图表情包,从此摇身变海王
    很多兄弟在聊天上没有下太多的功夫,导致自己聊天的时候很容易尬住,然后就不知道聊啥了,这时候合适表情包分分钟就能救场,但是一看自己收藏的表情包,好家伙,两只手都数得过来。......
  • python 读取excel表格中的数据
    有如下一张存储了数据的excel表,如下图所示,想要通过python代码将其中的数据提取出来方法步骤1、确定excel表格存放路径,这里以我的为例:/Users/Desktop/honops/USERRES/app......
  • Python学习day03
    一、内容回顾1.多行注释也等于多行打印python中单引号与双引号是一样的msg='''hello1hello2hello3'''print(msg)#结果:#hello1#hello2#hello31.for、if、while(br......