This question already has answers here:
Best way to keep track and iterate through tabs and windows using WindowHandles using Selenium
(1 answer)
Selenium switch focus to tab, which opened after clicking link
(3 answers)
Closed 2 years ago.
Still looking for resolution for following problem:
In Safari version 12: org.openqa.selenium.InvalidArgumentException: Request body does not contain required parameter 'handle'. I did see some solution saying use legacy, but not sure how to do that in selenium java.
Note: My code is working absolutely fine for Chrome, firefox, Edge browsers. I am facing this issue only with Safari.
Can anybody please help me here. Thanks in advance!
Selenium Version: 3.4/3.8
Code:
I am using Java for my scripts. Code:
String winHandleBefore = driver.getWindowHandle();
Set<String> windows = driver.getWindowHandles();
Assert.assertTrue("New browser window is not opened", windows.size() > 1);
for (String winHandle : driver.getWindowHandles()) {
String winHandleNew = winHandle;
if(!winHandleBefore.equalsIgnoreCase(winHandleNew)) {
driver.switchTo().window(winHandle);//THIS LINE GIVES THE ERROR
break;
}
}
driver.switchTo().defaultContent();
Related
Recently, I have upgraded my Selenium version from 2.53 to 4.1.2 to enable testing of our application on MS EDGE IE11. But we are intermittently facing issues while retrieving number of windows open in MS EDGE IE11 with selenium-4.1.2
Did anyone else facing similar kind of issues with Selenium-4.1.2 ?
Below is piece of code I have tried on MS EDGE IE11. Sometimes we could see its returning correct no. of windows but sometime not. We are also using sufficient wait-time before retrieving number of windows.
Note - This is working absolutely fine on IE11 browser with Selenium-4.1.2
int noOfWindowsOpen = driver.getWindowHandles().size();
Expectation : It should always return correct value of no. of windows open.
Once you open the new tab / window before you count the number of WindowHandles you need to induce WebDriverWait for numberOfWindowsToBe() as follows:
driver.get("http://www.google.com");
((JavascriptExecutor) driver).executeScript("window.open('http://facebook.com/');");
new WebDriverWait(driver, 10).until(ExpectedConditions.numberOfWindowsToBe(2));
int noOfWindowsOpen = driver.getWindowHandles().size();
It seems to be a known limitation in automating Edge IE mode. It says:
To ensure the new window has been created successfully and IEDriver
has detected it, you must continuously check the result of the Get
Window Handles command until it contains a handle to the new window.
You can try the sample code in it:
int initialHandleCount = driver.getWindowHandles().size();
driver.findElement(By.id("<Id of the button that will open a new window>")).click();
Set<string> newHandles = driver.getWindowHandles();
while (newHandles.size() == initialHandleCount) {
newHandles = driver.getWindowHandles();
}
I have found a work-around for above problem.
Before executing, make sure all the instances of ME Edge, IE and IE Driver are closed. If not, kill them forcefully from task manager and then re-run the Test Script. Script will identify the new window properly.
Thanks
This question already has answers here:
pageLoadTimeout in Selenium not working
(2 answers)
pageLoadTimeout is not working in Selenium - Java
(2 answers)
How to make selenium to reload the desired url if it takes too long loading
(3 answers)
Closed 2 years ago.
I've got a question:
Below there is my java code for selenium to load a url in chrome browser:
chromedriver is 84.0.4147.30
Chrome version is also 84
public static int navigate(String data) {
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
WebDriverWait wait = new WebDriverWait(driver, 30);
try {
driver.navigate().to(data);
wait.until(ExpectedConditions.invisibilityOfElementLocated(By.xpath("XPATH"));
....
}
What makes the automation:
when chrome is running the url is typed in the adreesbar and it loads the page.
But when the page is not loaded after 30 seconds, the function does not stop and aborts the test.
It waits endless.
I think it is the load sign in the page title tab that is the problem.
What can I do to abort the waiting when page is not loaded after 30 seconds?
Thanks in advance.
This question already has answers here:
Using already opened Browser window in Robot framework
(2 answers)
Closed 5 years ago.
For test cases using robot framework and Selenium2Library, we normally start with opening the browser and then doing actions on that browser.
But for the purpose of quickly testing steps within my test case (for testing my test itself) it would be nice if I could just run for example 'click button' on an already existing browser session.
This saves a lot of time if my AUT has to be in a certain screen and state to perform a certain action. That way I can automate and test a certain step in isolation without needing the script to perform all the steps required before that first.
Is there any selenium2libary keyword or some other way I might do this?
Thank you!
You do not need robotframe for same. You can check WindowHandles. If the windowHandle having value more then 0 then there is more window open then default one.
The code looks something like below. You can make changes in below if the window is more than 1. Try the following code:
String subWindowHandler;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
int i=handles.size();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
if(i>0) {
driver.switchTo().window(subWindowHandler); // switch to popup window
driver.close();
}
Hope it will help you :)
Meta -
OS: OSX El capitan
Selenium Version: 2.53.0
Browser: Safari
Browser Version: 9.1
SafariDriver version: 2.48.0 which is latest as per SeleniumHQ site.
Expected Behavior -
SafariDriver Launcher page/tab should get closed automatically and get switched to first tab where my URL is getting opened.
Actual Behavior -
Selenium is getting stuck on SafariDriver Launcher page[Tab] by not closing it and switching back to first Tab
Steps to reproduce -
Trying to execute n number of test cases from TestNG.xml, but few TCs are getting executed properly by closing SafariDriver Launcher page[tab].
But for few cases Selenium is getting stuck on SafariDriver Launcher page[Tab] by not closing it and switching back to first Tab where my URL will get opened.
Required Solution -
I wanna first check how many tabs are currently opened by:-
if(browser.equals("safari")){
capability=DesiredCapabilities.safari();
capability.setBrowserName("safari");
capability.setPlatform(Platform.MAC);
capability.setVersion("9.1");
capability.setJavascriptEnabled(true);
driver=new RemoteWebDriver(new URL(grid_url),capability);
driver.get(url);
Thread.sleep(2000);
String windowHandle = driver.getWindowHandle();
ArrayList tabs = new ArrayList (driver.getWindowHandles());
if(tabs.size()>1){
<***Need a solution here to close second tab and switch to first tab***>
driver.switchTo().window(tabs.get(0));
}
}
You can use this code to close the window you want to :
for(String handle : driver.getWindowHandles()) {
if (!handle.equals(originalHandle)) { //the window handle you want to close
driver.switchTo().window(handle);
driver.close(); // would close the specific tab you wanted
}
}
In your case originalHandle could be the handle for the current window (tab 2) you are at.
I have this problem with a text field that is visible at the time of the SendKeys. I'm using IEDriverServer.exe and C#.
Here's how I can reproduce the problem:
The text field in question is visible in the window but you have to scroll down to see it. To scroll down I click on the element using code like this:
var element = driver.FindElement(By.Xpath("…"));
element.Click();
This scrolls the window down and makes the text field visible.
But when I try to send text to now-visible window:
element.SendKeys("blah");
I get the exception:
When_applicant_enters_application.Should_be_instantly_approved_on_external threw exception: OpenQA.Selenium.ElementNotVisibleException: Element is not displayed
How can I fix or workaround this problem?
Selenium version: 2.32.1
OS: Windows 7
Browser: IE
Browser version: 9.0.15
I've written code demonstrating the problem and submitted it to the Selenium tech support volunteers.
The full discussion is at http://code.google.com/p/selenium/issues/detail?id=5620
but the take-home is:
// Doesn't work
// driver = new InternetExplorerDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
// driver = new FirefoxDriver();
// driver.Navigate().GoToUrl(#"D:\CGY\selenium\Bug5620\Bug5620\Bug5620.htm");
// Works
driver = new InternetExplorerDriver();
driver.Navigate().GoToUrl(#"http://localhost:8080/Bug5620/"); // Hosted on Tomcat
so there may be a problem that possibly involves IE, IE security settings, Visual Studio local servers and/or the IE Driver. This may not even be a code problem, but something that needs to be documented, since other people are apparently running into the problem.
I don't know where the problem is exactly but I do have a work-around at this point, which is to use the Firefox Driver.
Thanks for your help, Jim. If you find out a better way of dealing with the problem, please add an answer here for the other folks.