Bastian
Bastian

Reputation: 1237

Get text() in selenium not returning the text

I have a question regarding xpath. I try to get the text using the code shown in the screenshot.

I want to get the text from this xpath (Yes).

In console when I write:

//*[@id='info_is_network']/label[contains(@class , "ant-radio-button-wrapper ant-radio-button-wrapper-checked ant-radio-button-wrapper-disabled" )]/span/text(`)

It is OK and finds the element.

However in code when I do:

WebElement element = driver2.findElement(By.xpath("//*[@id='info_is_network']/label[contains(@class , \"ant-radio-button-wrapper ant-radio-button-wrapper-checked ant-radio-button-wrapper-disabled\" )]/span"));
String res = element.getText();
String res2 = element.getAttribute("value");

I get null or nothing in both attempts.

What can be the reason? No error in logs. Just copy from the dev tools to the intelij.

enter image description here

The Screen PIC (Buttons Not clickable, view only )

enter image description here

Upvotes: 1

Views: 1061

Answers (3)

Bastian
Bastian

Reputation: 1237

After Investigation this also do the work:

driver2.findElement(By.xpath("//*[@id='info_is_network']/label[contains(@class , \"ant-radio-button-wrapper ant-radio-button-wrapper-checked ant-radio-button-wrapper-disabled\" )]/span[text()]

Upvotes: 0

Sureshmani Kalirajan
Sureshmani Kalirajan

Reputation: 1938

The given xpath is finding the first span element.That explains why you are getting null. you have to get the following sibiling to get the correct text.try this xpath,

   WebElement element = driver2.findElement(By.xpath("//*[@id='info_is_network']/label[contains(@class , \"ant-radio-button-wrapper ant-radio-button-wrapper-checked ant-radio-button-wrapper-disabled\" )]/span/following-sibling:span"));
   String res = element.getText();

Upvotes: 3

supputuri
supputuri

Reputation: 14145

Try with below code.

element.getAttribute("textContent"));

Upvotes: 2

Related Questions