Home > database >  Selenium webdriver | driver().switchTo().defaultContent() method is not switching the control back t
Selenium webdriver | driver().switchTo().defaultContent() method is not switching the control back t

Time:11-02

Selenium webdriver | driver().switchTo().defaultContent() method is not switching the control back to parent window from multiple child windows.

I am facing problem with respect to no of windows and not frames. When I click a button on parent window say wp, a new web window say w1 is getting generated and eventually one more window gets generated say w2 , I want to switch to control to wp so I am using

driver.switchTo.defaultContent()

but its not switching control to parent window.

CodePudding user response:

There are two similar methods that can be used while switching to parent frames, but there is a slight difference between them.

driver.switchTo().defaultContent() driver.switchTo().parentFrame()

E.g.: You have three frames i1,i2,i3 and you are on frame i3, when you use driver.switchTo.defaultContent(), it will take you to i1, whereas with driver.switchTo.parentFrame() control will switch to i2 i.e. immediate parent frame (parent frame of the current frame).

CodePudding user response:

If the process opens multiple browser tabs, we need to use switchTo().window(window reference); to switch focus between the tabs.

// Save the initial tab as parentwindow 
String parentwindow = driver.getWindowHandle();

// Collect the tabs opened in a list
List<String> windows = new ArrayList<>(driver.getWindowHandles());

// Switch to window other than parentwindow
driver.switchTo().window(windows.get(1));

// Switch back to parentwindow
driver.switchTo().window(parentwindow);

CodePudding user response:

i have this problem to. and i find answer try this, and give me feedback - maybe i can help you

 const number1 = await driver.wait(until.elementLocated(By.xpath('your xpath')))
      await driver.executeScript("arguments[0].click()", number1).then(() =>{
        console.log(driver.executeScript, 'Click done');
      })
  • Related