定陶网站建设,学习编程,wordpress主题资讯,定州新闻今天重大新闻硒定位器是处理网页上的元素时的关键。 从ID#xff0c;名称#xff0c;类#xff0c;标记名#xff0c;XPath#xff0c;CSS选择器等定位器列表中#xff0c;可以根据需要选择其中任何一种#xff0c;然后在网页上找到Web元素。 由于与tagName或linktext相比#xff0… 硒定位器是处理网页上的元素时的关键。 从ID名称类标记名XPathCSS选择器等定位器列表中可以根据需要选择其中任何一种然后在网页上找到Web元素。 由于与tagName或linktext相比ID名称XPath或CSS选择器的使用更为频繁因此人们对后者的定位器的了解较少或没有工作经验。 在本文中我将详细介绍Selenium中tagName定位器的用法和实时示例。 那么Selenium中的tagName定位符是什么 tagName是DOM结构的一部分其中页面上的每个元素都是通过标签例如输入标签按钮标签或锚定标签等定义的。每个标签都具有多个属性例如ID名称值类等。就其他定位符而言在Selenium中我们使用了标签的这些属性值来定位元素。 对于Selenium中的tagName定位器我们将仅使用标签名称来标识元素。 以下是LambdaTest登录页面的DOM结构其中我突出显示了标签名称 电子邮件字段 input typeemail nameemail value placeholderEmail requiredrequired autofocusautofocus classform-control mt-3 form-control-lg input typeemail nameemail value placeholderEmail requiredrequired autofocusautofocus classform-control mt-3 form-control-lg 密码栏位 input typepassword namepassword placeholderPassword classform-control mt-3 form-control-lg input typepassword namepassword placeholderPassword classform-control mt-3 form-control-lg 登录按钮 button typesubmit classbtn btn-primary btn-lg btn-block mt-3LOGIN /button button typesubmit classbtn btn-primary btn-lg btn-block mt-3LOGIN /button 忘记密码链接 button typesubmit classbtn btn-primary btn-lg btn-block mt-3LOGIN /button button typesubmit classbtn btn-primary btn-lg btn-block mt-3LOGIN /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 srchttps://www.gravatar.com/avatar/daf7dc69b0d19124ed3f9bab946051f6.jpg?s200dmmrg altsadhvi classimg-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_fielddriver.findElement(By.name( email )); //Entering text into the email field //Entering text into the email field email_field.sendKeys( sadhvisingh24gmail.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_fielddriver.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_buttondriver.findElement(By.xpath( //button[text()LOGIN] )); //Clicking on the login button login_button.click(); //Clicking on the Settings option driver.findElement(By.xpath( //*[idapp]/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( //*[idapp]/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 ListWebElement 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 j0; ji; j) { //Printing the links System.out.println(links.get(j).getText()); } //Closing the browser driver.close(); } } 下面是控制台的屏幕截图 情况3 在此示例中我将展示何时要标识表中的行数因为在运行时此信息可以是动态的因此我们需要预先评估行数然后检索或验证信息。 下面是表的DOM结构 tbody classyui3-datatable-data tr idyui_patched_v3_18_1_1_1554473988939_130 data-yui3-recordmodel_1 classyui3-datatable-even tr idyui_patched_v3_18_1_1_1554473988939_130 data-yui3-recordmodel_1 classyui3-datatable-even td classyui3-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 ListWebElement 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