1

I'm having some trouble with an element not being able to be located using an xpath and was looking for some input if anyone may know what the problem might be. This is the element that I'm trying to access.

<body id="tinymce">
  <p>
    <img class="some-class-name" src="/path/to/url" data-macro-name="macro-name" data-macro- 
     parameters="macro|params" data-macro-schema-version="1">
    <span id="_caret" data-mce-bogus="true" style=""><u></u></span>
  </p>
</body>

and this is the pageobject code that I have to find and use it.

@FindBy(xpath = "//*[@id=\"tinymce\"]/p/img")
private WebElement cnfGraph;

public void verifyGraphEditPresent()  {
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOf(cnfGraph));
}

I keep getting the following error

no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="tinymce"]/p/img"}

I've tried also changing the locator to the class name of the image or the id of the body, but nothing's picking it up.

Any help or advice is appreciated. Thanks

Ethranes
  • 329
  • 1
  • 5
  • 22
  • 1
    Your opening body tag is missing the `>`, was that a typo or is the code actually like that? Also have you checked for iframes? Have you tried using `'` instead of `\"`? – MentallyRecursive Dec 03 '19 at 15:21
  • Was a type, have updated it. Looking further up the html I can see that all of this is inside an iframe. Will try changing the syntax in the xpath locator to ' and see if that helps – Ethranes Dec 03 '19 at 15:24
  • 1
    If the code is inside an iframe you will need to switch the driver context to that iframe, and your original xpath will probably work just fine. See this link: https://stackoverflow.com/a/24254740/11865571 – MentallyRecursive Dec 03 '19 at 15:26
  • https://github.com/SeleniumHQ/selenium/issues/4385 suggests that you need to use ExpectedConditions.visibilityOfElementLocated – LoflinA Dec 03 '19 at 15:29
  • If you need a wait for the frame, I would suggest looking into `ExpectedConditions.frameToBeAvailableAndSwitchToIt` – MentallyRecursive Dec 03 '19 at 15:33
  • 1
    Switching to the iframe has fixed my issue, thanks for the info,a valuable lesson learned! – Ethranes Dec 03 '19 at 15:43

1 Answers1

1

I was able to resolve this issue by switching the driver to the iframe the html was in.

public void verifyGraphEditPresent()  {
    driver.switchTo().frame(driver.findElement(By.id("wysiwygTextarea_ifr")));
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOf(cnfGraph));
    driver.switchTo().defaultContent();
} 

Solution is from here https://stackoverflow.com/a/24254740/11865571 provided by @MentallyRecursive in the comments

Ethranes
  • 329
  • 1
  • 5
  • 22