java - Switch active tabs with Selenium -
this question has answer here:
i have developed code used open search results new tab:
string selectlinkopeninnewtab = keys.chord(keys.control, keys.return); results.get(i).sendkeys(selectlinkopeninnewtab); (int = 0; < results.size(); i++) { arraylist<string> tabs2 = new arraylist<string>(driver.getwindowhandles()); driver.switchto().window(tabs2.get(1)); driver.close(); driver.switchto().window(tabs2.get(0)); }
what want when open tabs search results want switch between tabs 2 seconds delay.
how can implement this?
try code,
import java.awt.awtexception; import java.awt.robot; import java.awt.event.keyevent; import java.util.arraylist; import java.util.list; import org.openqa.selenium.by; import org.openqa.selenium.keys; import org.openqa.selenium.webdriver; import org.openqa.selenium.webelement; import org.openqa.selenium.chrome.chromedriver; import org.openqa.selenium.support.ui.expectedconditions; import org.openqa.selenium.support.ui.webdriverwait; public class tabswitching { public static void main(string[] args) throws interruptedexception, awtexception { system.setproperty("webdriver.chrome.driver", "d:/workspace/driver/chromedriver.exe"); webdriver driver = new chromedriver(); driver.get("http://www.google.com/xhtml"); thread.sleep(5000); webelement searchbox = driver.findelement(by.name("q")); searchbox.sendkeys("software testing"); searchbox.submit(); system.out.println("current url: " + driver.getcurrenturl()); webdriverwait wait = new webdriverwait(driver, 10); wait.until(expectedconditions.presenceofelementlocated(by.cssselector("h3.r a"))); list<webelement> results = driver.findelements(by.xpath("//h3[@class='r']/a")); (webelement ele : results) { string selectlinkopeninnewtab = keys.chord(keys.control, keys.return); ele.sendkeys(selectlinkopeninnewtab); } thread.sleep(5000); arraylist<string> tabs2 = new arraylist<string>(driver.getwindowhandles()); system.out.println(tabs2.size()); (int = tabs2.size()-1; i>=0; i--) { thread.sleep(2000); driver.switchto().window(tabs2.get(i)); robot robot = new robot(); robot.keypress(keyevent.vk_control); robot.keypress(keyevent.vk_tab); robot.keyrelease(keyevent.vk_tab); robot.keyrelease(keyevent.vk_control); system.out.println(driver.gettitle() + "i: " + i); driver.close(); } } }
Comments
Post a Comment