I am using Selenium for Scrapping purpose for Job Website scrapping. I am ctrl+clicking on jobs to go to second page and scrape details, I somehow wish to keep track of where I clicked to which tab opened up (using a map). But as soon as I ctrl+click an element, new tab opens and focus changes to that.
How do I get handle of the current focused tab in Selenium
I tried
webDriver.getWindowHandle()
But it returns me the handle for main tab that I ctrl+clicked an element from.
My Full Code
private void openAndMapJobTabs(WebElement jobList) {
List<WebElement> jobRowsSelector = jobList.findElements(By.tagName("article"));
for (WebElement jobRow : jobRowsSelector) {
try {
ctrlClickElement(jobRow);
Thread.sleep(4000);
//the next line is expected by me to give handle of current focused tab
System.out.println(webDriver.getWindowHandle()); // but this gives me handle of main tab
webElementToTabMapper.put(jobRow, webDriver.getWindowHandle());
webDriver.switchTo().window(websiteTabHandle);
} catch (Exception e) {
logger.info("Could Not Click Open Job Tab: "+e.getMessage());
}
}
}
Any help world be highly appreciated. Also if there are faster and better tools for scrapping, please recommend.