在Selenium中按TagName定位元素

本文介绍了Selenium中如何使用tagName定位器,详细解释了何时在没有其他属性值时依赖此定位器,并通过多个实时场景展示了其在定位页面元素、验证链接数量和识别表格行数等方面的应用。
摘要由CSDN通过智能技术生成

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值