Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to identify and switch to the frame in selenium webdriver when frame does not have id

Can anyone tell me how I can identify and switch to the iframe which has only a title?

<iframe frameborder="0" style="border: 0px none; width: 100%; height: 356px; min-width: 0px; min-height: 0px; overflow: auto;" dojoattachpoint="frame" title="Fill Quote" src="https://tssstrpms501.corp.trelleborg.com:12001/teamworks/process.lsw?zWorkflowState=1&zTaskId=4581&zResetContext=true&coachDebugTrace=none"> 

I have tried by below code but it is not working

driver.switchTo().frame(driver.findElement(By.tagName("iframe"))); 
like image 295
Sunil wali Avatar asked Nov 19 '13 11:11

Sunil wali


People also ask

How do I switch to a specific frame in Selenium?

Another way to switch between frames in selenium is to pass the WebElement to the switchTo() command. Here the primary step is to find the iframe element and then pass it to the switch method.

Which method can be used to switch to a frame using ID of the frame?

frame(int frameNumber) method allows the users to switch to a particular frame using the frame id. switchTo. frame(string frameName) method allows the users to switch to a particular frame using the developer-defined name of the frame.

Which of the following method is used to switch back from a frame in Selenium?

We can switch back from a frame to default in Selenium webdriver using the switchTo(). defaultContent() method. Initially, the webdriver control remains on the main web page. In order to access elements within the frame, we have to shift the control from the main page to the frame with the help of the switchTo().


2 Answers

driver.switchTo().frame() has multiple overloads.

  1. driver.switchTo().frame(name_or_id)
    Here your iframe doesn't have id or name, so not for you.

  2. driver.switchTo().frame(index)
    This is the last option to choose, because using index is not stable enough as you could imagine. If this is your only iframe in the page, try driver.switchTo().frame(0)

  3. driver.switchTo().frame(iframe_element)
    The most common one. You locate your iframe like other elements, then pass it into the method.

Here locating it by title attributes seems to be the best.

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Fill Quote']"))); // driver.switchTo().frame(driver.findElement(By.xpath(".//iframe[@title='Fill Quote']"))); 
like image 96
Yi Zeng Avatar answered Nov 04 '22 08:11

Yi Zeng


you can use cssSelector,

driver.switchTo().frame(driver.findElement(By.cssSelector("iframe[title='Fill Quote']"))); 
like image 27
Amith Avatar answered Nov 04 '22 07:11

Amith