Santhosh Siddappa
Santhosh Siddappa

Reputation: 718

How to get Text from this HTML code By using Selenium WebDriver with Java

HTML Code

<label for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21">Royality Free</label>

Selenium Code

driver.findElement(By.id("ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21")).getText();

The above selenium code is not working even i tried getAttribute(); its showing NullPointerException

Upvotes: 0

Views: 2180

Answers (1)

Abhijeet Vaikar
Abhijeet Vaikar

Reputation: 1656

You are trying to read text from the label but you are finding an element which has id ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 This is not the id of the label.

Your code should be:

WebElement labelElement = driver.findElement(By.cssSelector("label[for="ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21"]"));
System.out.println(labelElement.getText());

This should work.

Moreover, the locator: ctl00_ContentPlaceHolder1_RadPanelBar1_i0_chkColumns_21 seems to be a randomly generated locator. Just confirm that it's not such a case. If it is then you will need to change your locating strategy.

Upvotes: 1

Related Questions