0

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

2
  • Can you please post webpage url or complete HTML code snippet? Commented Mar 25, 2014 at 4:57
  • One option would be to use its its parent or ancestors to reach label Commented Mar 25, 2014 at 5:28

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.