I'm currently working on Selenium tests for a web platform, and I've noticed that the test have problems finding elements on a certain page which will make the test fail. The problem occurs once the test have pressed a button, which opens up a new tab, so my question is whether or not the reason for this happening is due to the webdriver being set to the first tab in the webbrowser or is it something else?
PS.The test has no problem finding elements if the driver is set to start on the second page.
You have to switch to a new tab, because driver would find only the actual tab WebElements.
Switch to a new tab by using:
ArrayList<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.close(); //to close actual tab
driver.switchTo().window(tabs.get(1)); //then switch to new tab
Your suggestion sounds correct, to work with the new tab you have to switch driver to it.
This can be simply done like this:
public void switchToNewWindow(){
wait(500);
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(tabs.size()-1));
}
to switch back to the first window you can use this:
public void switchToFirstWindow(){
List<String> tabs = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(tabs.get(0));
}
In case there are more than 2 tabs you can switch to any tab as shown above switching to tab with the corresponding index.
Related
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());
}
}
}
UPDATE
Actually for me, I need to map elements to the tab they open, so order of tabs are important for me, so I purposely need the handle of current focused tab so I can map it with the element which opened that. But by iterating through all handels, I might mess up the order if any one of the webElement fails to open a new tab or if click is disabled on that.
Any help world be highly appreciated.
Also if there are faster and better tools for scrapping, please recommend.
You have to get the handles of those two tabs or windows, then you have to iterate and switch to the newly opened tab. Try the below code:
// This line will get the handles of the all the tabs or windows and store in a var with type 'Set'
Set<String> windowHandles = driver.getWindowHandles();
// Iterating through the Set - i.e., window handles and switch to the other tab
for (String handle : windowHandles) {
// System.out.println(handle);
driver.switchTo().window(handle);
System.out.println("Title: " + driver.getTitle());
}
If you have more than two tabs or windows, then put the above code the in a method, pass the window title which you want to switch as a parameter, and compare the title with each window title, once you switch to the expected window, break.
I'm trying to automate a functionality where I have to open a new tab and start to work on this page, but my code still looking the WebElements on the last Tab. I try to switch the tabs with the follow code but its not working.
public void DataManager() throws InterruptedException {
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(0));
driver.close();
driver.switchTo().window(tabs2.get(1));
WebElement tes = driver.findElement(By.xpath("//*[#id=\"lui-popover-3\"]/div/ng-transclude/ul/li[2]/button[1]/span[2]"));
tes.click();
}
What's going on, and how can I fix this?
1st time, You have focus driver on 1st tab which is on 0 index and you have close it. After you close it, Your 1 index become 0 index but you have invoke it on 1 index.
You may Refer sample example:
ArrayList<String> tabs= new ArrayList<String>(driver.getWindowHandles());
js.executeScript("window.open()");
driver.switchTo().window(tabs.get(1));
Here, by this lines New tab will be open by keeping current Tab. And driver will invoke for Tab-1.
I found the solution, I had a problem to find Web Elements in my page, so when I used the following code to change the selenium focus:
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
And I searched the Web Element using press Tab I found it.
Thanks!
I am trying to automate a scenario where when I click on a link another tab opens with details.
Question 1 : Do I have to specifically set my focus to the 2nd tab or selenium automatically finds the element in the 2nd tab?
I am using the below code to set the focus to the 2nd tab :
String currentWindow = driver.getWindowHandle();
driver.switchTo().window(currentWindow);
Problem : I am getting an error that selenium is unable to find the specified element.
Could you guys suggest me what am I doing wrong, and the best way to switch to 2nd tab.
Actually, you are setting the focus on the first tab, not the second one. You need to do something like this
String currentWindow = driver.getWindowHandle();
// open the new tab here
for (String handle : driver.getWindowHandles()) {
if (!handle.equals(currentWindow)) {
driver.switchTo().window(handle);
}
}
And the answer to your question is yes, you have to tell the driver to set its focus on the new tab.
You can get all the window handles as handlers=driver. GetWindowHandles() which will return all the handler string. Then using index switch to the appropriate handle using driver.switchto().window(handlers[1])
I am using Selenium with Java, and I am using this code to switch between tabs in Mozilla, but it is opening a new window instead of a new tab. How do solve this, or is there another way to switch between tabs?
WebDriver shiva=new FirefoxDriver();
shiva.manage().window().maximize();
shiva.get("http://www.naukri.com/");
Thread.sleep(3000);
shiva.findElement(By.xpath("/html/body/div[1]/div/ul[1]/li[2]/a")).click();
shiva.findElement(By.xpath("/html/body/div[1]/div/ul[1]/li[1]/a")).sendKeys(Keys.CONTROL +"\t");
Try to use this code:
ArrayList<String> tabs = new ArrayList<>(webDriver.getWindowHandles());
webDriver.switchTo().window(tabs.get(1)); // id of the tab
In a normal firefox window, a new window is opened in a new tab, if you go to settings --> General --> Tabs you will see an option Open new window in new tab instead
But when Selenium Webdriver launches a firefox profile, this option isn't selected by default so it opens in a new window instead of a new tab.
If you want to open a new tab, you need to create a different firefox profile with this option enabled and then you can launch the created profile
There is another way by which you can switch to a different tab.
Set<String> listOfTabs = driver.getWindowHandles();
// This code will return a set with all the window ids
// Then you can switch on any of the window.
driver.switchTo.window("String Id of window");
Your code should be below for switching between Tabs: Assert the relevant tab based on pageTitle (I Guess)
shiva.findElement(By.xpath("/html/body/div[1]/div/ul[1]/li[1]/a")).sendKeys(Keys.CONTROL + Keys.PAGE_DOWN);
If you wish to switch between windows then use
driver.switchto().window(windowhandle);
Also I wonder how are you able to open multiple Tabs?
Use firefox version 80.0 or above
I think it is a bug and it is resolved in firefox 80.0
I too had the same issue(using firefox version- 78.0 back then) but once i changed the version to 80 or above,it worked flawlessly
Am Opening a page performing some actions on that and i am using this piece of code to open another link in the next tab
String url = "https://qa.logfireapps.com/lgf_700_qa_rf";
String args1 = String.format("window.open('%s', '%s');", url, "new");
((JavascriptExecutor) driver).executeScript(args1);
Then i need to switch between this two tabs.
I used driver.switchTo.window(parentWin);
I also used this code
List windowHandles1 = new ArrayList(driver.getWindowHandles());
driver.switchTo().window(windowHandles1.get(1));
Both of the cases did not work for me,but still the web driver code is running successfully without any errors even its not switching to first window.
I need to switch to the first tab,but all the actions are going on in the first tab but still in UI am seeing the second tab opened,It is not switching to the first tab but my whole webdriver code gets passed.
It is observed that switching problem happens only with this two sites
1) https://qa.logfireapps.com/lgf_700_qa/index/
2) https://qa.logfireapps.com/lgf_700_qa_rf
This was my solution to this using python. sorry no java example
def switch_to_new_window(driver, window):
driver.switch_to_window(driver.window_handles[window])