硒定位器是处理网页上的元素时的关键。 从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

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

  1. 在Selenium中按TagName定位元素

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

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

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

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

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

  4. java中常用的定位方式有哪些_java-selenium八种元素定位方式

    一.ID定位 一般情况下页面元素的id属性在当前网页中是唯一的所以使用ID定位可以保证定位的唯一性,不会像其他定位方式一样可能定位到多个页面元素.但有的网页页面元素没有id属性值,导致无法使用ID定位 ...

  5. 在硒中查找具有链接文本和部分链接文本的元素

    Selenium中CSS定位器是一个基本概念,每个旨在使用Selenium执行自动化测试的测试人员都应该意识到这一点. 在Selenium中充分使用CSS定位器可以帮助您以更高效,更彻底的方式执行测试 ...

  6. python定位元素在列表中的位置_python定位列表元素

    Python 列表(list)提供了 index() 和 count() 方法,它们都可以用来查找元素. index() 方法 index() 方法用来查找某个元素在列表中出现的位置(也就是索引),如 ...

  7. CSS中浮动和定位对元素宽度/外边距/其他元素所占空间的影响

    一.width:auto和width:100%的区别 1.width:100%的作用是占满它的参考元素的宽度.(一般情况下参考元素 == 父级元素,这里写成参考元素而不是父级元素,在下面我会再细说) ...

  8. 在鼠标悬停才会出现的页面中定位元素

    在实现web自动化的过程中,有时候会碰到元素在鼠标悬停后才会出现(一般是顶部导航栏菜单),移走鼠标后页面收缩,没办法去定位元素的信息. 这种情况可以使用以下方法来进行定位: 鼠标悬停到标签上后,在So ...

  9. 在ios中fixed定位元素丢失,Date兼容NaN

    Aphorism Preface 差不多有半年没有更新blog了, 现在在新的公司适应了,最近喜得一千金,也慢慢适应了新的生活. 今天更新一篇关于最近遇到的 ios fixed定位元素丢失 bug i ...

最新文章

  1. 面对500篇GNN论文,心态差点儿崩了,幸好我有这本小书
  2. C#_获取文件路径中的文件名_扩展名
  3. 时序分析:串匹配-KMP算法
  4. 外部服务发现之 ingress(一) traefik 的安装使用
  5. 【sqoop】sqoop概念,功能,架构,版本
  6. 如何在Git上创建工程,演示在Git中创建项目
  7. Junit第一次使用
  8. C# datagridview绑定Liststring显示的是数据长度
  9. Java并发编程实战_《Java并发编程实战》PDF版本下载
  10. 钉钉H5应用开发-jsapi调用
  11. oracle sql列转行_Oracle列转行函数使用
  12. 我在云栖社区读硕士,大数据专业
  13. python我国有13亿人口、假定按人口年增长0.8%计算_2000年世界人口50亿.按年增长率8%0计算.多少年后.世界人口超过100亿.请设计出一个算法.并画出程序框图....
  14. Debian 安装手记
  15. 有限覆盖定理证明其他实数完备性定理
  16. 【论文阅读】Color Constancy by Learning to Predict Chromaticity from Luminance
  17. Mybatis在集群环境下脏读问题
  18. Arduino使用TM1637 4位数码管模块
  19. 什么是RTOS?RTOS与普通操作系统的区别
  20. 高级 JavaScript Day04 | 正则表达式

热门文章

  1. [POI2015] Pustynia(差分约数,线段树优化建图,拓扑)
  2. 80%的程序员都不了解的调试技巧
  3. WEB攻击手段及防御-扩展篇
  4. Maven精选系列--继承与聚合
  5. C++描述杭电OJ 2009.求数列的和 ||
  6. 中控指纹采集器开发指纹识别项目(说明)
  7. 想对你们每个人说的话
  8. [置顶]动态网页开发基础【笔记】
  9. Linux下安装nginx (tar解压版安装) nginx1.16.1
  10. 如何在Intellij IDEA中集成Gitlab