Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select a frame using selenium?

I'm using Java to create selenium test cases. My system is based on portlets connected to each other. I'm using "selectFrame" command to select the portlet.

I tried many things but it seems it is not working like this:

driver.switchTo().frame("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]");

driver.findElement(By.id("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]"));

Can anyone help me?

like image 595
2ousy Avatar asked Dec 15 '22 07:12

2ousy


2 Answers

You have an XPath expression that is supposed to get you the IFrame element you need. However you are not telling Selenium it's an XPath expression. The below is what you need:

driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]"));

Note, my Java is not it's best, so this may cause compilation issues but you should see the idea.

Find the element first, by telling Selenium it's an XPath expression you are giving it, then use that element and stick it right in the 'switch to frame' expression.

like image 60
Arran Avatar answered Feb 08 '23 02:02

Arran


driver.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.xpath("//iframe[contains(@src,'FUN_UnitList_FilterByLevelIndexOne')]")));
like image 35
jgode Avatar answered Feb 08 '23 04:02

jgode