java - How to update a FirefoxDriver (WebDriver) object when opening, and switching between tabs? -
long time lurker; first time poster. i'm new selenium api , webdriver, , i'm having small problem.
in short, attempting leverage firefox tab feature using selenium's firefoxdriver, driver instance object not returning correct url using getcurrenturl() method when switching between tabs. here brief example of trying accomplish:
firefoxdriver driver = new firefoxdriver(); driver.get("http://www.google.com"); // display starting tab url system.out.println(driver.getcurrenturl()); // expected output: google.com webelement body = driver.findelement(by.cssselector("body")); // open new tab if(system.getproperty("os.name").contains("mac")) { body.sendkeys(keys.command + "t"); } else { body.sendkeys(keys.control + "t"); } //navigate in new tab driver.get("http://www.yahoo.com"); // display new tab url system.out.println(driver.getcurrenturl()); // expected output: yahoo.com //navigate previous tab body = driver.findelement(by.cssselector("body")); body.sendkeys(keys.control +""+keys.shift +""+ keys.tab); // display starting tab url system.out.println(driver.getcurrenturl()); // expected output: google.com driver.close(); however, on execution output reads:
yahoo
yahoo
my intuition suggests driver's frame/tab view not being updated enough, or @ all, not sure how determine this. appreciated.
thanks!
i found solution/workaround correct driver's current view. after driver switches different tab, call switchto().defaultcontent() must made.
the working code follows:
firefoxdriver driver = new firefoxdriver(); driver.get("http://www.google.com"); // display starting tab url system.out.println(driver.getcurrenturl()); // expected output: google.com webelement body = driver.findelement(by.cssselector("body")); // open new tab if(system.getproperty("os.name").contains("mac")) { body.sendkeys(keys.command + "t"); } else { body.sendkeys(keys.control + "t"); } //navigate in new tab driver.get("http://www.yahoo.com"); // display new tab url system.out.println(driver.getcurrenturl()); // expected output: yahoo.com //navigate previous tab body = driver.findelement(by.cssselector("body")); body.sendkeys(keys.control +""+keys.shift +""+ keys.tab); // refresh driver view driver.switchto().defaultcontent(); // display starting tab url system.out.println(driver.getcurrenturl()); // expected output: google.com driver.close(); the new resulting output matches expected:
yahoo
Comments
Post a Comment