A test case where image and content are to be added in WP text editor under post section.

I want to select the image that is uploaded in the editor but it always says "Unable to locate element"

XPath I tried is : //*[@id="tinymce"]/p[2]/img

enter image description here


enter image description here

  • 1
    Its in iframe have you switched into that frame before click on that image ?
    – NarendraR
    Jan 29, 2018 at 7:55
  • No I didn't, let me try that too. Thanks
    – Rabia Asif
    Jan 30, 2018 at 5:42
  • Great! your suggestion worked. Thanks again
    – Rabia Asif
    Jan 30, 2018 at 5:45
  • Yes! i was trying to automate Amazon... and faced same issue with image.... Thanks
    – shilpa pai
    Jul 24, 2018 at 7:30
  • 1
    @NarendraR you should post your solution as an answer to get your upvote and make this question be resolved.
    – Alexey R.
    Jul 24, 2018 at 9:43

2 Answers 2


Your image element is in iFrame, So first you need to switch into related iFrame and then you can perform desired actions on that element.

There are following ways provided in the Selenium to work with iframes :

Switch using frame Index

driver.switchTo().frame(0); // used indexes 0,1... as number of frames you have in webpage

Switch using frame id (if your iframe have id attribute)


Switch using webelement (locate the frame as webelement)

WebElement frame = driver.findElement(By.xpath("frame_xpath"));

Apart from these you can also use ExplicitWait conditions to switch into frame e.g.

new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("your_frame_id")));

In your case you have to switch in and perform your actions e.g. :


You can use driver.switchTo().defaultContent(); to move out from frame.


From looking into the html code it seems there is an iframe from which you are accessing the element. In this automation testing services issue is related to accessing the element inside an iframe. Selenium treats iframe like a window. So you first need to switch to that window(which is an iframe here). You need to switch to iframe first and then search for the element you were looking for.

Below is the simple code related to for reference using this not only you any one could fix such type of issues:

First get the iframe name from the html code and then use webdriver method to switch to the iframe as (say iframe id is 'first-frame')

// Switch to frame

// now search for the element you want to search

// Now switch back to main window which is done by this code.

Last code snippet is necessary to access other elements outside from the iframe.

Hope that helps.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.