在Selenium中按TagName定位元素

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/332458.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python open读取_python,一读取文件open()

在实际操作中&#xff0c;我们经常会读取文件&#xff0c;这个时候python为我们提供了一个open()的方法&#xff0c;供我们读取文件&#xff0c;通过help(open)&#xff0c;我们可以获取open的方法 f.close()关闭读取 f.read(size-1)读取文件size个字符&#xff0c;但未给size赋…

笔记本计算机风扇声音大怎么办,笔记本电脑噪音大怎么办 全解决方法

笔记本电脑在使用一段时间后&#xff0c;或多或少会产生令人厌烦的噪音。其中大的噪音多数来源于笔记本散热风扇。那么&#xff0c;笔记本电脑噪音大怎么办呢?下面我们来看具体解决方法吧。一、电脑噪音大怎么办之风扇原因在不少人看来&#xff0c;散热和噪音是一对不可调和的…

Maven的resources插件配置详解(含过滤器的配置详解)

文章目录指定配置文件所在的目录使用 excludes 元素可以排除指定的配置文件使用 includes 元素可以指定要处理的文件处理测试资源过滤器配置resources 插件&#xff0c;负责将配置文件复制到编译目录中。Maven Java Web 项目默认的编译目录 target/classes。两种配置文件 src/…

latex如何使节标题居左_为使节构建控制平面的指南第3部分-特定于域的配置API...

latex如何使节标题居左这是探索为Envoy Proxy构建控制平面的系列文章的第3部分。 在本博客系列中&#xff0c;我们将研究以下领域&#xff1a; 采用一种机制来动态更新Envoy的路由&#xff0c;服务发现和其他配置 确定哪些组件构成了控制平面&#xff0c;包括后备存储&#…

微信支付api的服务器上,服务器微信支付接口笔记(与app端对接)

到这里&#xff0c;准备工作就算完成了。支付流程步骤详解&#xff1a;步骤1&#xff1a;用户在商户APP中选择商品&#xff0c;提交订单&#xff0c;选择微信支付。这一步&#xff0c;app将相关订单信息提交给商户步骤2&#xff1a;商户后台收到用户支付单&#xff0c;调用微信…

python写sql语句_Python操作文件模拟SQL语句功能

一、需求 当然此表你在文件存储时可以这样表示 1,Alex Li,22,13651054608,IT,2013-04-01 现需要对这个员工信息文件&#xff0c;实现增删改查操作 1. 可进行模糊查询&#xff0c;语法至少支持下面3种: 1. select name,age from staff_table where age > 22 2. select * from…

Maven的maven-clean-plugin插件详解

maven-clean-plugin 插件对应的命令是 mvn clean&#xff0c;执行 mvn clean 命令会删除构建输出目录 target。 打开命令终端&#xff0c;切换到 pom.xml 所在目录下&#xff0c;执行下面的命令&#xff1a; [~/documents/IdeaProjects/demo02]$ mvn clean [INFO] Scanning f…

jep290涉及jdk版本_JDK 12 – JEP 325开关表达式

jep290涉及jdk版本JDK 12已于2019年3月19 日上线GA&#xff0c;继续致力于缩短发布周期和频繁发布。 该版本的功能部分可以在这里找到。 对于开发人员来说&#xff0c;有趣的功能之一是“ JEP 325开关表达式 ”&#xff0c;它可以作为预览功能使用。 此处定义的预览功能是&…

笑傲江湖客户端服务器地址修改,《笑傲江湖》改键调整操作手把手教你玩笑傲...

《笑傲江湖》采用全新引擎AngelicaIII打造&#xff0c;秉承原著武侠精髓&#xff0c;首推新派动作武侠网游概念——融入动作及格斗游戏要素&#xff0c;强调真实的打击感与流畅的动作连贯度&#xff0c;并运用方向判定、位移闪避、移动战斗、攻防一体等多重技术手段&#xff0c…

Maven的maven-compiler-plugin插件详解

文章目录mvn compilemvn test-compile编译插件的配置mvn compile mvn compile 命令会将 src/main/resources 下的资源文件复制到编译输出目录下&#xff1b;接着会将 src/main/java 目录下源代码编译输出到编译输出目录下。编译输出目录默认是 target/classes 目录。 打开命令…

python数据处理常用函数_pytorch中的自定义数据处理详解

pytorch在数据中采用Dataset的数据保存方式&#xff0c;需要继承data.Dataset类&#xff0c;如果需要自己处理数据的话&#xff0c;需要实现两个基本方法。 &#xff1a;.getitem:返回一条数据或者一个样本&#xff0c;obj[index] obj.getitem(index). :.len:返回样本的数量 。…

raid重构原理_5个重构原理示例

raid重构原理这篇文章介绍了重构真正的开源代码&#xff08; Gradle Modules Plugin &#xff09;时应用的五​​种&#xff08;最著名的&#xff09;重构原理。 语境 当我为Gradle Modules Plugin &#xff08;PR &#xff03;73 &#xff09; 单独编译 module-info.java &am…

extjs ajax 遮罩层,[Ext JS 4] 实战之Load Mask(加载遮罩)的显示与隐藏

前言Load Mask(遮罩)效果&#xff0c;就是在页面还没有完全显示出来之前&#xff0c; 加上一个转装转的效果。类似&#xff1a;添加这样的效果有两个好处&#xff1a;1. 在页面没完全show出来之前&#xff0c; 把后面的页面给遮罩起来&#xff0c; 防止进行一些非法的操作。2. …

macOS下卸载文件系统_卸载移动硬盘_卸载U盘_推出移动硬盘_推出U盘

先使用命令 df -lh 在终端查看当前系统的所有挂载的文件系统&#xff08;系统硬盘、移动硬盘、U盘等&#xff09;&#xff0c;命令如下&#xff1a; liaowenxiongliaowenxiongdeMacBook-Air ~ % df -h Filesystem Size Used Avail Capacity iused ifree %iused …

python isalnum函数_Python 字符串 (isdigit, isalnum,isnumeric)转

Python isdigit() 方法检测字符串是否只由数字组成。 语法 isdigit()方法语法&#xff1a; str.isdigit() 参数 无。 返回值 如果字符串只包含数字则返回 True 否则返回 False。 Python isdecimal() 方法检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。 注意:定…

zing jvm_Zing加快了JVM应用程序的预热

zing jvmJava虚拟机&#xff08;JVM&#xff09;提供了托管运行时环境&#xff0c;用于安全部署应用程序&#xff0c;其性能通常可以超过本机编译语言&#xff08;如C和C &#xff09;。 通过即时&#xff08;JIT&#xff09;编译进行垃圾回收和自适应编译的内存管理是两个最突…

黎明觉醒测试服服务器维护怎么办,黎明觉醒测试资格进不去怎么办

黎明觉醒测试资格进不去怎么办&#xff1f;黎明觉醒游戏在在9月16日迎来曙光测试&#xff0c;相信不少玩家都去玩了发现自己进不去游戏&#xff0c;这是怎么回事呢&#xff1f;和小编一起来看看吧。黎明觉醒测试资格进不去怎么办一、测试资格进不去获得测试资格的玩家官方已提前…

查看Linux命令_搜索Linux命令_查找Linux命令

站点1&#xff1a;https://tool.lu/command/ 站点2&#xff1a;https://www.linuxcool.com/

蜂鸣器音乐代码 天空之城_潮玩 | 艺术展览,乐队live现场,网红小黑泥,贩卖“美好”的市集……一场未来公共生活,天空之城和你一起探索!...

第一次打卡这样的新媒体艺术作品&#xff0c;不是画作&#xff0c;也不是艺术品陈列&#xff0c;而是一场看的见的引力交响曲~错落的磁场具象成看得见的流动痕迹&#xff0c;不动声响却震撼的感官体验。很容易让人沉浸其中&#xff0c;去捕捉流动的方向和瞬间。虽然UFO是没看到…

自动部署 管道 ci cd_自动化测试在CI CD管道中的作用

自动部署 管道 ci cd业界广泛采用的软件开发实践&#xff1a;持续集成和持续部署可确保良好地交付产品并经常交付。 常规代码提交需要常规/连续测试&#xff0c;而如果忽略它&#xff0c;则可能导致非弹性基础结构。 如何交付坚固的CI CD管道&#xff1f; 对于许多公司来说&…