Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I select elements inside an iframe with Xpath?

I want to create a Selenium test to test our extensions with AOL mail. I managed to login to AOL and compose an email, but I also need to select elements inside the editor, which is inside an iframe. I checked and even when the editor is open the following test fails:

self.assertEqual(first=1, second=len(self.driver.find_elements_by_xpath(xpath="//iframe[@name='editor_body']//body[@contenteditable='true']")))

I get the error AssertionError: 1 != 0. How do I select the body of the iframe and other elements by Xpath (or in any other way with Selenium)?

like image 815
Uri Avatar asked Mar 02 '15 16:03

Uri


People also ask

How do I select an element in an iframe?

Getting the element in Iframe const iframe = document. getElementById("myIframe"); Now, it has and contentWindow property which returns the document object by using that we can access the elements from an Iframe. const iWindow = iframe.

What is XPath in iframe?

XPath is a language that describes a way to locate and process items in HTML And XML documents by using an addressing syntax based on a path through the document's logical structure or hierarchy. If you want to get element`s XPath of selected element of Iframe and also add highlight css in element on mouseover , out and click event. a.

How to select elements inside an iframe with XPath in Selenium WebDriver?

We can select elements inside an iframe with xpath in Selenium webdriver. A frame is defined with <iframe>, <frameset> or <frame> tag in html code. A frame is used to embed an HTML document within another HTML document. Let us see the html code of a frame. Selenium by default has access to the parent browser driver.

How to access elements inside the frame using the XPath locator?

Once we move the driver focus inside the frame, we can access the elements inside the frame by the xpath locator with the help of the driver.findElement (By.xpath (<xpath value>)) method. Code Implementation.

Where can I find the try it button in an iframe?

Let's try writing the test commands to find the "Try it" button and then to click it. That button is located inside the body element of the document of the iframe element. Let's write a helper function to get to the body element.


2 Answers

You cannot traverse through <iframe>'s until switching to them. Your xPath,

//iframe[@name='editor_body']//body[@contenteditable='true']

will not work because the <body> tag is within an iFrame, which is not in the current context. you need to switch to it first:

driver.switch_to.frame('editor_body')...
like image 84
ddavison Avatar answered Oct 17 '22 05:10

ddavison


In case you are facing issues during Selenium automation testing here is some info that solved my problem:

Selenium by default has access to the parent browser driver. In order to access inside a frame, the driver's focus has to shift from the main browser window to the iframe(frame).

Therefore, if you happen to have to do assertions inside an iframe and then go back to the main window to do other interactions you will need to change the Driver's focus based on your target.

Read more at: https://www.tutorialspoint.com/how-do-i-select-elements-inside-an-iframe-with-xpath

like image 1
Guilherme Borges Bastos Avatar answered Oct 17 '22 03:10

Guilherme Borges Bastos