Prophet
Prophet

Reputation: 33361

Selenium Webdriver: Can't access element containing onclick and text attributes

The HTML page I'm working with contains the following element:

<a style="text-decoration:none; font-weight:normal;" href="javascript:void(0);" onclick="CreateNewServiceItemApproved();">  
<img src="icons/ui/addnew.png">  
<span style="color:#000">Add New Incident</span>  
</a>  

I need to click on it but receiving

 org.openqa.selenium.NoSuchElementException: Unable to locate element

error.
Tried to locate it with following commands:

driver.findElement(By.xpath("a[@onclick='CreateNewServiceItemApproved()']")).click();  
driver.findElement(By.xpath("//span[contains(text(),'Add New Incident')]"));  

Why can't I locate this element?
What's wrong here?

Upvotes: 0

Views: 658

Answers (1)

Fran Montero
Fran Montero

Reputation: 1680

As the element is inside a iframe, you need to switchto the iframe first:

driver.switchTo().frame(driver.findElement(By.id("iframeId")));

and then find the <a> element.

Iframe can be found by id, xpath or the usual methods.

When processing is donde, you could need to switch to default content with:

driver.switchTo().defaultContent();

Upvotes: 2

Related Questions