3

I am trying to click on a download link which will open in a new tab in pdf format.I want to do some actions on newly open tab.

I am able to switch to newly open tab but when I am trying to do any operation its giving exception "no such element exception" bcoz of still it is focused on previous tab

driver.get("http://somewebsite.com/");
driver.findElement(By.id("download")).click();  //By clicking on this it will open a new tab 
for(String winHandle:driver.getWindowHandles()){
    driver.switchTo().window(winHandle);
}
8
  • In question, Please put code which you tried Aug 28, 2015 at 8:41
  • It should work , still you can use wait command before click on element of that newly open tab. Aug 28, 2015 at 10:45
  • Its resolved by adding sleep statement after click statement.Thanks @Helping Hands
    – QAMember
    Aug 28, 2015 at 12:09
  • 2
    @QAMember I would look into WebDriverWait and ExpectedConditions rather than using Thread.sleep. (If that's what you meant)
    – Julian
    Aug 28, 2015 at 15:56
  • @QAMember - That's great to hear... Aug 31, 2015 at 2:48

3 Answers 3

0

Here it is the working code for switching between two tabs

driver.get("http://somewebsite.com/");
driver.findElement(By.id("download")).click();  
thread.sleep(3000);
for(String winHandle:driver.getWindowHandles()){
    driver.switchTo().window(winHandle); 
}
1
  • using a static sleep is bad practice Dec 12, 2016 at 19:45
0

I would recommend something like this. Sometimes on window change there is no guarantee you have window focus such as when browser is configured to NOT change to new tab when new tabs open:

driver.get("http://somewebsite.com/");
driver.findElement(By.id("download")).click();  
Sleeper.sleepTight(3000); // new window should have focus at this point
getLatestWindowFocused(driver);

There are a million ways you can do this, but here is an example, and it all depends on what happens to work for you:

public static void getLatestWindowFocused(WebDriver driver) 
{
    String mostRecentWindowHandle;
    for(String winHandle:driver.getWindowHandles()){
        mostRecentWindowHandle = winHandle;        
    }
    driver.switchTo().window(mostRecentWindowHandle);
    JavascriptExecutor js = (JavascriptExecutor)driver;
    js.executeScript("window.focus();");
    js = null;
}
2
  • Hello @djangofan, Thanks for your enhanced answer .Can you please provide some explanation about below line "myUtilityClass.forceWindowFocusWithJavascriptExecutor(driver);"
    – QAMember
    Sep 7, 2015 at 4:48
  • Ok, I updated it. The answer is meant for you to read between the lines.
    – djangofan
    Sep 7, 2015 at 17:21
-2
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
1
  • 2
    Code without explanation (and formatting) is usually not good. Please consider to add some information.
    – beatngu13
    Apr 4, 2018 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.