Selenium定位器是处理网页上的元素时的关键。 从ID,名称,类,标记名,XPath,CSS选择器等定位器列表中,可以根据需要选择其中任何一种,然后在网页上找到Web元素。 由于与tagName或linktext相比,ID,名称,XPath或CSS选择器的使用更为频繁,因此人们大多对后一种定位器不太了解或没有工作经验。 在本文中,我将详细介绍Selenium中tagName定位器的用法和实时示例。

那么,Selenium中的tagName定位符是什么?

tagName是DOM结构的一部分,其中页面上的每个元素都是通过输入标签,按钮标签或锚定标签等标签定义的。每个标签都具有多个属性,例如ID,名称,值类等。就其他定位符而言在Selenium中,我们使用了标签的这些属性值来定位元素。 对于Selenium中的tagName定位器,我们将仅使用标签名称来标识元素。

以下是LambdaTest登录页面的DOM结构,其中我突出显示了标签名称:

电子邮件字段: < input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg"> < input type="email" name="email" value="" placeholder="Email" required="required" autofocus="autofocus" class="form-control mt-3 form-control-lg">

密码栏位: < input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg" > < input type="password" name="password" placeholder="Password" class="form-control mt-3 form-control-lg" >

登录按钮: < button type="submit" class="btn btn-primary btn-lg btn-block mt-3">LOGIN< /button > < button type="submit" class="btn btn-primary btn-lg btn-block mt-3">LOGIN< /button >

忘记密码链接: < button type="submit" class="btn btn-primary btn-lg btn-block mt-3">LOGIN< /button > < button type="submit" class="btn btn-primary btn-lg btn-block mt-3">LOGIN< /button >

现在出现的问题是,何时在Selenium中使用此tagName定位符? 好吧,在没有属性值(如ID,类或名称)并且倾向于定位元素的情况下,您可能不得不依靠在Selenium中使用tagName定位器。 例如,如果您希望从表中检索数据,则可以使用< td >标记或< tr >标记检索数据。

同样,在希望验证链接数量并验证它们是否正常工作的情况下,您可以选择通过anchor标签定位所有此类链接。

请注意:在一个简单的基本场景中,仅通过标签定位元素,这可能会导致识别大量值并可能导致问题。 在这种情况下,Selenium将选择或定位与您端提供的标签匹配的第一个标签。 因此,如果要定位单个元素,请不要在Selenium中使用tagName定位器。

通过Selenium中的tagName标识元素的命令是:

 driver.findElement(By.tagName( "input" )); 

实时场景在Selenium中突出显示tagName定位器

场景1

一个基本的示例,我们在LambdaTest的“我的个人资料”部分中找到图像头像:

参考是化身的DOM结构:

< img src="https://www.gravatar.com/avatar/daf7dc69b0d19124ed3f9bab946051f6.jpg?s=200&d=mm&r=g" alt="sadhvi" class="img-thumbnail" >

现在让我们看一下代码片段:

 package Chromedriver;  import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.WebElement;  import org.openqa.selenium.chrome.ChromeDriver;  public class Locator_By_Tagname { public static void main(String[] args) throws InterruptedException { // TODO Auto-generated method stub         //Setting up chrome using chromedriver by setting its property //Setting up chrome using chromedriver by setting its property System.setProperty( "webdriver.chrome.driver" , " Path of chromeDriver " );                 //Opening browser WebDriver driver= new ChromeDriver() ;                 //Opening in maximize mode //Opening window tab driver.manage().window().maximize();                 //Opening application driver.get( " https://accounts.lambdatest.com/login " );                 //Locating the email field element via Name tag and storing it //Locating the email field element via Name tag and storing it in the webelement WebElement email_field=driver.findElement(By.name( "email" ));                 //Entering text into the email field //Entering text into the email field email_field.sendKeys( "sadhvisingh24@gmail.com" );                 //Locating the password field element via Name tag and storing it //Locating the password field element via Name tag and storing it in the webelement WebElement password_field=driver.findElement(By.name( "password" ));                         //Entering text into the password field //Entering text into the password field password_field.sendKeys( "New1life" );                 //Clicking on the login button to login to the application WebElement login_button=driver.findElement(By.xpath( "//button[text()='LOGIN']" ));                 //Clicking on the 'login' button login_button.click();                 //Clicking on the Settings option driver.findElement(By.xpath( "//*[@id='app']/header/aside/ul/li[8]/a" )).click();                 //Waiting for the profile option to appear Thread. sleep (3500);                 // *[@ id = "app" ] /header/aside/ul/li [8] /ul/li [1] /a //Clicking on the profile link driver.findElement(By.xpath( "//*[@id='app']/header/aside/ul/li[8]/ul/li[1]/a" )).click();                 //Locating the element via img tag for the profile picture and storing it in the webelement WebElement image= driver.findElement(By.tagName( "img" ));                 //Printing text of Image alt attribute which is sadhvi System.out.println(image.getAttribute( "alt" )); }  } 

方案2

在此示例中,我们将验证LambdaTest主页上的链接数并打印这些链接文本:

 package Chromedriver;  import java.util.List;  import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.WebElement;  import org.openqa.selenium.chrome.ChromeDriver;  public class Tagname_linktest { public static void main(String[] args) { // TODO Auto-generated method stub //Setting up chrome using chromedriver by setting its property //Setting up chrome using chromedriver by setting its property System.setProperty( "webdriver.chrome.driver" , " Path of chromeDriver " );                 //Opening browser WebDriver driver= new ChromeDriver() ;                 //Opening in maximize mode //Opening window tab driver.manage().window().maximize();                 //Opening application driver.get( " https://www.lambdatest.com " );                 //storing the number of links in list List<WebElement> links= driver.findElements(By.tagName( "a" ));                 //storing the size of the links int i= links.size();                 //Printing the size of the string System.out.println(i);                 for (int j=0; j<i; j++) { //Printing the links System.out.println(links.get(j).getText()); }                 //Closing the browser driver.close();                                 }  } 

以下是控制台的屏幕截图:


场景3

在此示例中,我将展示何时要标识表中的行数,因为在运行时此信息可以是动态的,因此,我们需要事先评估行数,然后检索或验证信息。

下面是表的DOM结构:

< tbody class="yui3-datatable-data" >< tr id="yui_patched_v3_18_1_1_1554473988939_130" data-yui3-record="model_1" class="yui3-datatable-even" >

< tr id="yui_patched_v3_18_1_1_1554473988939_130" data-yui3-record="model_1" class="yui3-datatable-even">< td class="yui3-datatable-col-name yui3-datatable-cell ">John A. Smith< /td >

1236 Some Street San Francisco CA< /td >< /tr >

//……更多行继续//

现在,让我们看一下其代码片段:

 package Chromedriver;  import java.util.List;  import org.openqa.selenium.By;  import org.openqa.selenium.WebDriver;  import org.openqa.selenium.WebElement;  import org.openqa.selenium.chrome.ChromeDriver;  public class Tagname_Row_data { public static void main(String[] args) { // TODO Auto-generated method stub //Setting up chrome using chromedriver by setting its property //Setting up chrome using chromedriver by setting its property System.setProperty( "webdriver.chrome.driver" , "Path of chromeDriver" );         //Opening browser WebDriver driver= new ChromeDriver() ;         //Opening in maximize mode //Opening window tab driver.manage().window().maximize();         //Opening application driver.get( " https://alloyui.com/examples/datatable " );         //locating the number of rows of the table List<WebElement> rows= driver.findElements(By.tagName( "tr" ));         //Printing the size of the rows System.out.print(rows.size());         //Closing the driver driver.close();                                 }  } 

控制台输出快照:


结论

如您所见,我如何在Selenium中的不同情况下使用tagName定位器。 您还可以使用XPath或CSS选择器将tagName定位器与属性值结合使用。 当涉及到其他定位元素的场景时,我可能不建议您在Selenium中使用tagName定位器,但是上述提到的场景确实可以派上用场。 Selenium中tagName定位器的使用可能受到限制,但是,如果您希望成为熟练的自动化测试人员 ,那么了解如何使用tagName定位器以及何时使用它就变得非常关键。


翻译自: https://www.javacodegeeks.com/2019/04/locating-elements-tagname-selenium.html

在Selenium中按TagName定位元素相关推荐

  1. 在硒中按TagName定位元素

    硒定位器是处理网页上的元素时的关键. 从ID,名称,类,标记名,XPath,CSS选择器等定位器列表中,可以根据需要选择其中任何一种,然后在网页上找到Web元素. 由于与tagName或linktex ...

  2. python 地址模糊匹配_使用python处理selenium中的xpath定位元素的模糊匹配问题

    # 用contains,寻找页面中style属性值包含有sp.gif这个关键字的所有div元素,其中@后面可以跟该元素任意的属性名. self.driver.find_element_by_xpath ...

  3. Selenium自动化测试:8种元素定位+unittest框架设计

    作者简介: 笔名,唐米.参与过汇丰银行,国家电网,中国电信等多个大型项目的研发和管理,擅长的技术领域为安全测试,性能测试,自动化框架搭建与维护,曾受南京航空航天大学邀请分享Linux.oracle等测 ...

  4. js下拉 selenium_selenium 难定位元素,时间插件,下拉框定位,string

    1.元素定位 ID定位元素: findElement(By.id("")); 通过元素的名称定位元素: findElement(By.name("")); 通过 ...

  5. selenium中的三种等待方法

    在selenium中,当我们定位的元素由于网络原因还没加载出来,浏览器找不到我们要定位的元素就会报错.等待方式的设置是保证脚本稳定有效运行的一个非常重要的手段,常用的等待方法有: sleep() -- ...

  6. 在selenium中使用css选择器进行元素定位(一)

    大家在使用selenium元素定位的时候,通常更多使用的是XPATH,css定位方式用得比较少 但有时候css定位方式还是有一些优势的, 优势1:一般情况下定位速度要比XPATH快 优势2:语法要比X ...

  7. selenium课程笔记3-使用selenium中的webdriver对浏览器操作-页面元素定位及操作

    1.打开浏览器,打开网页 1 from selenium import webdriver 2 dr=webdriver.Chrome() 3 dr.get('http://www.baidu.com ...

  8. Selenium中元素定位方法详细介绍

    一.元素定位基本方法 1.如何进行元素定位? 元素:由标签头 + 标签尾 + 标签头和标签尾包括的文本内容: 元素的信息就是指元素的标签名及元素的属性: 元素的层级结构就是指元素之间相互嵌套的层级结构 ...

  9. selenium 中隐藏元素如何定位?

    前言 面试题:selenium 中隐藏元素如何定位?这个是很多面试官喜欢问的一个题, 如果单纯的定位的话,隐藏元素和普通不隐藏元素定位没啥区别,用正常定位方法就行了 但是吧~~~很多面试官自己都搞不清 ...

最新文章

  1. 1020 Tree Traversals
  2. linux安装google chrome
  3. 【网络爬虫】BeautfulSoup爬百度百科(真の能看懂~!)
  4. 【活动】PMcaff免费培训之终结版扩招名额----20名额等你来抢
  5. VTK:单元格内部对象CellsInsideObject用法实战
  6. 微软(MS Dynamics SL)Solomon 承包商用户2006年度会议!
  7. POJ 1703 Find them, Catch them 种类并查集
  8. phpcms图片无法上传
  9. python常胜将军问题_Python中最常见的10个问题(列表)
  10. 用getchar和%C输入字符型数据
  11. Qt文件打包_vortex_新浪博客
  12. 问题三十七:C++怎么解一元四次方程?(2)——怎么解一元三次方程
  13. LoaderManager使用具体解释(四)---实例:AppListLoader
  14. linux服务器一键可视化,安装宝塔教程
  15. URL转换成IP的过程
  16. python 修改文件名有特殊符号_Linux删除包含特殊符号文件名的文件
  17. 13.相机和图像——视场(Field of View),视场实战_4
  18. 使用Dir函数遍历文件和目录
  19. tensor的storage(),stride(),storage_offset()
  20. npm私服发包及使用

热门文章

  1. 1022. 宠物小精灵之收服
  2. 花店橱窗布置(洛谷P1854)(动态规划)
  3. 【2018.3.24】模拟赛之二-ssl2546 求和【贪心】
  4. 【树链剖分】Milk Visits G(luogu 5838)
  5. 【结论】游戏(jzoj 1984)
  6. 2017西安交大ACM小学期 刁钻的顾客[3进制+折半枚举]
  7. Eclipse 4.9 正式发布,支持 Java 11
  8. 用eclipse创建动态web项目手动生成web.xml方法
  9. 19级、20级:班级日常分享,一天一瞬间
  10. 《四世同堂》金句摘抄(四)