在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
總結
以上是生活随笔為你收集整理的在Selenium中按TagName定位元素的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: java开发指南_Java 12新功能完
- 下一篇: 8月买的电脑八月首发的电脑