java - Open Google links into new tab -
i want open google search results new tab. tried code:
webdriver driver = new chromedriver(); driver.get("http://www.google.com/xhtml"); thread.sleep(5000); webelement searchbox = driver.findelement(by.name("q")); searchbox.sendkeys("chromedriver"); 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")); (int = 0; < results.size(); i++) { string selectlinkopeninnewtab = keys.chord(keys.control,keys.return); driver.findelement(by.xpath("//h3[@class='r']/a")).sendkeys(selectlinkopeninnewtab); system.out.println("google result " + results.get(i).gettext()); }
so far code opens empty tabs , prints name of search results. how can open links searh results?
just change below line inside loop
:-
driver.findelement(by.xpath("//h3[@class='r']/a")).sendkeys(selectlinkopeninnewtab);
to
results.get(i).sendkeys(selectlinkopeninnewtab);
it's working...:)
Comments
Post a Comment