3

I'm trying to get the image which has the word "Collector" in its title and click on it. This is the html code for the image and its link:

<a href="javascript:*command*" title="Level III: KPI Collector RYG of D D/Testing - SYS">
<img src="*unusable link because it's only valid for the specific page*" title="Level III: KPI Collector RYG of D D/Testing - SYS">

The <a> and <img> tags are nested in a table cell and some divs. I didn't write the html code so don't yell at me if it's ugly :p
Here is the java code where I try to do it:

WebElement trafficLight = driver.findElement(By.xpath("//img[contains(@title,'Collector')]"));
trafficLight.click();

The error I get is:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: {"method":"xpath","selector":"//img[contains(@title,'Collector')]"}

I'm pretty sure the xpath is ok so I don't think that's the issue.

3
  • 1
    Check if the element is inside a frame Commented Jul 7, 2015 at 13:02
  • Yes, it seems like it is in an <iframe>. The image location hierarchy is like this: <ton of divs>-><iframe> <body> -> <table> -> <tr> -> <td> -> <div> -> <span> -> <a> -> <img>. I'm guessing there's a different way of getting it then? Commented Jul 7, 2015 at 13:19
  • The element was in an <iframe>. To access it, I used the answer from here: stackoverflow.com/questions/24247490/… Commented Jul 7, 2015 at 13:25

2 Answers 2

3

As the img WebElement is within a frame, you will need to switch focus to that frame before you perform any action on that WebElement. You can do that using WebDriver's switchTo() method like so:

driver.switchTo().frame(frameLocator);

The frame locator can be either its (zero-based) index, name or id attribute, or a previously located WebElement.

Once you have switched focus to the required frame, you should then be able to interact with the WebElement using the same code in your initial post.

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

Comments

3

Please try this. It will resolve your problem.

WebElement frameSwitch = driver.findElement(By.xpath("Give iframe Xpath location")); 
driver.switchTo().frame(frameSwitch); //Switch control to iframe.

//Perform your steps (I.e Click on Image)
driver.findElement(By.xpath("//img[contains(@title,'Collector')]")).click();

driver.switchTo().defaultContent(); //Come out of iframe.

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.