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);
}
  • In question, Please put code which you tried – Helping Hands Aug 28 '15 at 8:41
  • It should work , still you can use wait command before click on element of that newly open tab. – Helping Hands Aug 28 '15 at 10:45
  • Its resolved by adding sleep statement after click statement.Thanks @Helping Hands – QAMember Aug 28 '15 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 '15 at 15:56
  • @QAMember - That's great to hear... – Helping Hands Aug 31 '15 at 2:48
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); 
}
| improve this answer | |
  • using a static sleep is bad practice – Corey Goldberg Dec 12 '16 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;
}
| improve this answer | |
  • Hello @djangofan, Thanks for your enhanced answer .Can you please provide some explanation about below line "myUtilityClass.forceWindowFocusWithJavascriptExecutor(driver);" – QAMember Sep 7 '15 at 4:48
  • Ok, I updated it. The answer is meant for you to read between the lines. – djangofan Sep 7 '15 at 17:21
-2
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
| improve this answer | |
  • 2
    Code without explanation (and formatting) is usually not good. Please consider to add some information. – beatngu13 Apr 4 '18 at 18:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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