Reputation: 1237
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.
The Screen PIC (Buttons Not clickable, view only )
Upvotes: 1
Views: 1061
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
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