How to select elements inside an iframe with xpath?

Member

by domenico , in category: Javascript , 3 months ago

How to select elements inside an iframe with xpath?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by kadin , 3 months ago

@domenico 

To select elements inside an iframe using XPath, you need to first switch to the iframe context and then use the XPath expression to locate the desired elements. Here is an example of how you can achieve this in Selenium WebDriver:

  1. Switch to the iframe context:
1
2
WebElement iframeElement = driver.findElement(By.xpath("//iframe[@id='iframe-id']"));
driver.switchTo().frame(iframeElement);


  1. Once you have switched to the iframe context, you can use XPath to locate elements inside the iframe:
1
2
WebElement elementInsideIframe = driver.findElement(By.xpath("//div[@class='element-class']"));
// Perform actions on the element


  1. After you are done working with elements inside the iframe, you can switch back to the default content:
1
driver.switchTo().defaultContent();


By following these steps, you can effectively select elements inside an iframe using XPath in Selenium WebDriver.