2017-08-09 138 views
0

下午好,我正在尝试使用Eclipse IDE和Java语言自学Selenium Webdriver。所以,我有以下要素,我已通过其ID发现​​Selenium - 查找元素类名

<button role="tab" id="tabs-id_1-link-1" class="c-tabs__link is-active" aria-selected="true" aria-controls="tabs-id_1-article-1">**</button> 

我是很新,硒,但到目前为止,我用它来寻找元素,

WebElement Currentbilltab = driver.findElement(By.id("tabs-id_1-link-1")); 

但我会再喜欢找出这个元素的类是否包含active这个词。什么是最好的方法来做到这一点?任何帮助将不胜感激。

+0

清楚地提及代码。 – vishal

回答

0

您可以使用下面的API

WebElement.getAttribute("attribute"); 

为你的情况

Currentbilltab.getAttribute("class"); 
0

使用下面的代码获得元素的任何属性。它会识别文本您体上,进行打印: -

String text = driver.findElement(By.xpath("//button[@id='tabs-id_1-link-1']")).getText(); 
System.out.println(text); 

如果你想获得任何属性值,然后使用: -

String text = driver.findElement(By.id("tabs-id_1-link-1")).getAttribute("class"); 
System.out.println(text); 

在上面的代码中类的值将可以打印出

希望它会帮助你:)

0

如果你想获得的元素的class属性与idtabs-id_1-link-1您可以使用以下代码块:

//Store the WebElement 
WebElement Currentbilltab = driver.findElement(By.xpath("//button[@id='tabs-id_1-link-1']")); 

//Retrieve the class attribute of the WebElement in a String 
String element_class = Currentbilltab.getAttribute("class"); 

//Print the String which contains the class attribute of the WebElement 
System.out.println("Class name of the element is: "+element_class);