日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

在Selenium中按TagName定位元素

發(fā)布時間:2023/12/3 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 在Selenium中按TagName定位元素 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

Selenium定位器是處理網(wǎng)頁上的元素時的關(guān)鍵。 從ID,名稱,類,標(biāo)記名,XPath,CSS選擇器等定位器列表中,可以根據(jù)需要選擇其中任何一種,然后在網(wǎng)頁上找到Web元素。 由于與tagName或linktext相比,ID,名稱,XPath或CSS選擇器的使用更為頻繁,因此人們大多對后一種定位器不太了解或沒有工作經(jīng)驗。 在本文中,我將詳細(xì)介紹Selenium中tagName定位器的用法和實(shí)時示例。

那么,Selenium中的tagName定位符是什么?

tagName是DOM結(jié)構(gòu)的一部分,其中頁面上的每個元素都是通過輸入標(biāo)簽,按鈕標(biāo)簽或錨定標(biāo)簽等標(biāo)簽定義的。每個標(biāo)簽都具有多個屬性,例如ID,名稱,值類等。就其他定位符而言在Selenium中,我們使用了標(biāo)簽的這些屬性值來定位元素。 對于Selenium中的tagName定位器,我們將僅使用標(biāo)簽名稱來標(biāo)識元素。

以下是LambdaTest登錄頁面的DOM結(jié)構(gòu),其中我突出顯示了標(biāo)簽名稱:

電子郵件字段: < 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 >

現(xiàn)在出現(xiàn)的問題是,何時在Selenium中使用此tagName定位符? 好吧,在沒有屬性值(如ID,類或名稱)并且傾向于定位元素的情況下,您可能不得不依靠在Selenium中使用tagName定位器。 例如,如果您希望從表中檢索數(shù)據(jù),則可以使用< td >標(biāo)記或< tr >標(biāo)記檢索數(shù)據(jù)。

同樣,在希望驗證鏈接數(shù)量并驗證它們是否正常工作的情況下,您可以選擇通過anchor標(biāo)簽定位所有此類鏈接。

請注意:在一個簡單的基本場景中,僅通過標(biāo)簽定位元素,這可能會導(dǎo)致識別大量值并可能導(dǎo)致問題。 在這種情況下,Selenium將選擇或定位與您端提供的標(biāo)簽匹配的第一個標(biāo)簽。 因此,如果要定位單個元素,請不要在Selenium中使用tagName定位器。

通過Selenium中的tagName標(biāo)識元素的命令是:

driver.findElement(By.tagName( "input" ));

實(shí)時場景在Selenium中突出顯示tagName定位器

場景1

一個基本的示例,我們在LambdaTest的“我的個人資料”部分中找到圖像頭像:

參考是化身的DOM結(jié)構(gòu):

< img src="https://www.gravatar.com/avatar/daf7dc69b0d19124ed3f9bab946051f6.jpg?s=200&d=mm&r=g" alt="sadhvi" class="img-thumbnail" >

現(xiàn)在讓我們看一下代碼片段:

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主頁上的鏈接數(shù)并打印這些鏈接文本:

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

在此示例中,我將展示何時要標(biāo)識表中的行數(shù),因為在運(yùn)行時此信息可以是動態(tài)的,因此,我們需要事先評估行數(shù),然后檢索或驗證信息。

下面是表的DOM結(jié)構(gòu):

< 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 >

//……更多行繼續(xù)//

現(xiàn)在,讓我們看一下其代碼片段:

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(); ????????????????????????????????} }

控制臺輸出快照:

結(jié)論

如您所見,我如何在Selenium中的不同情況下使用tagName定位器。 您還可以使用XPath或CSS選擇器將tagName定位器與屬性值結(jié)合使用。 當(dāng)涉及到其他定位元素的場景時,我可能不建議您在Selenium中使用tagName定位器,但是上述提到的場景確實(shí)可以派上用場。 Selenium中tagName定位器的使用可能受到限制,但是,如果您希望成為熟練的自動化測試人員 ,那么了解如何使用tagName定位器以及何時使用它就變得非常關(guān)鍵。


翻譯自: https://www.javacodegeeks.com/2019/04/locating-elements-tagname-selenium.html

總結(jié)

以上是生活随笔為你收集整理的在Selenium中按TagName定位元素的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。