1

I am a newbie to java and selenium webdriver. I am having an issue clicking an image. Below is the page source.

<a href="javascript:void(0);">
<span class="HomeButton" onclick="javascript:onBtnHomeClick();"/>
</a>

I tried below codes but did not work and still getting the Unable to locate element error.

driver.findElement(By.xpath("//a[@onclick='onBtnHomeClick()']")).click();

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click();


wait.until(ExpectedConditions.visibilityOfElementLocated(By.className("HomeButton"))).click();

I have to click the homebutton. Any help would be much appreciated

2
  • Click on Upvote / Accept, for , my answer if my locator works. provided this is your application functionality it will always work even if minor changes will be present. Also this is one of your best strategies for non break ability. Commented Mar 1, 2016 at 16:07
  • Any luck with my Xpath Commented Mar 2, 2016 at 13:39

2 Answers 2

1

I don't know why By.className("HomeButton") didn't work but you have errors in the other two.

In driver.findElement(By.xpath("//a[@onclick='onBtnHomeClick()']")).click(); the tag for onclick is <span> not <a>. It also not onBtnHomeClick() but javascript:onBtnHomeClick();

driver.findElement(By.xpath("//span[@onclick='javascript:onBtnHomeClick();']")).click();

If you want to use onBtnHomeClick() use contains

driver.findElement(By.xpath("//span[contains(@onclick, 'onBtnHomeClick')]")).click();

Or

driver.findElement(By.cssSelector("onclick*='onBtnHomeClick'")).click();

And in wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='js_AppContainer']/div[2]/div[1]/div[1]/span"))).click(); the <span> parent tag is <a>, not <div>

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[@id='js_AppContainer']/div[2]/div[1]/div[1]/a/span"))).click();
Sign up to request clarification or add additional context in comments.

Comments

0

You simply need the correct locator IF your element will be eventually visible.

Xpath = "//span[contains(@class,'HomeButton') and contains(@onclick,'onBtnHomeClick')]"

Add wait as needed above exanmple, that should work.

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.