I need to automate a scenario where an application link opens a new window and I need to interact with that window. I have been successful in doing this however the success rate I've encountered is about 75%, where the other 25% causes problems in that I can't interact with the newly opened window. Here is my current solution.
// Click the link to open the new window
driver.findElement(By.linkText("Link")).click();
Thread.sleep(1000); // Sleep for 1 second
// Switch to the new window
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
Thread.sleep(1000);
driver.manage().window().maximize(); // Maximise the new window
I have experimented playing around with the sleep timers but these don't seem to help. I am also using the Selenium Internet Explorer WebDriver.
In instances which I can't interact with the newly opened window, the window also does not maximize if that gives any indication of my problem.
Many thanks.
// Click the link to open the new window
driver.findElement(By.linkText("Link")).click();
Thread.sleep(1000); // Sleep for 1 second
// Switch to the new window
for (String winHandle : driver.getWindowHandles()) {
driver.switchTo().window(winHandle);
}
// After switching to the new window.
// wait for some time either use thread.sleep or better to wait on a condition like follows :
WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.elementToBeClickable(By.id("btnNext")));
// then make the window to maximize
driver.manage().window().maximize();
You can use ChromeOptions to open the browser always in Maximised mode.
OR
Use this driver.manage().window().maximize(); as soon as you open the browser connection so that when ever you open/ try to navigate to a new links it gets maximised automatically.
Example for chrome driver as shown below;
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--start-maximized");
System.setProperty("webdriver.chrome.driver","Path to chromedriver.exe");
driver = new ChromeDriver(chromeOptions);
driver.get("http://google.com");
Related
I want run one test script. The scenario is like that;
Go to login page
Login with valid credentials
Close current single tab
Go to the login page again
Verify whether browser will go homepage instead of login page or not.
But i don't know how can i do that. Because if i use close or quit method, session id will be killed. even if i open new browser for another step, it will not be same with real scenario. Because as manually, if i try to go to login page after closing single open homepage without logging out, i can go to home page directly. I am not be able to automate it.
I used some Actions method to close and also open new tab.
And i used these;
driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL+”t”);
driver.findElement(By.cssSelector(“body”)).sendKeys(Keys.CONTROL + 'w');
I am using Selenium+Java+Cucumber+Chrome+Mac
You can use profile to achieve this scenario. I 've just given some sample code:
WebDriverManager.chromedriver().setup();
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-data-dir=C:\\Temp\\");
options.addArguments("--profile-directory=ChromeData");
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://www.flipkart.com/");
driver.findElement(By.xpath("//input[#class='_2IX_2- VJZDxU']")).sendKeys("<username>");
driver.findElement(By.xpath("//input[#class='_2IX_2- _3mctLh VJZDxU']")).sendKeys("<password>");
Thread.sleep(1000);
driver.findElement(By.xpath("//button[#class='_2KpZ6l _2HKlqd _3AWRsL']")).click();
Thread.sleep(3000);
driver.close();
Thread.sleep(1000);
driver = new ChromeDriver(options);
driver.manage().window().maximize();
driver.get("https://www.flipkart.com/");
The above code will create a new chrome profile in the directory - "C:\Temp\ChromeData", then open the browser, launch the URL, login, close the window, again open a chrome browser, launch the same URL, but this time, the user already logged in.
I'm trying to switch to a new window that will open when I login to the previous page. After clicking the login button a new tab will be open and gets closed immediately then a new window is open, this window handle is not recognized by the Selenium IE driver in IE MODE but I'm able to switch to this new window while automating in normal Internet Explorer browser. Selenium version is 4.3.0.0.
The code that I'd tried to switch to that new windows:
Thread.sleep(3000); // This delay is to avoid the new tab that gets closed immediately without this delay the driver is trying to switch to that new tab, after that any operation leads to throwing no browser exception. So this line is saving from the issue.
String desiredTitle = "";
while (!desiredTitle.contains("new window")) {
for (String ewh: driver.getWindowHandles()) {
desiredTitle = driver.switchTo().window(ewh).getTitle();
}
}
There're known limitations in Selenium 4 for IE mode automation. One of the workarounds suggests waiting until the driver gets the handle. You can try the sample code provided in that doc.
Currently in my tests when i select a button a pop up appears asking me to launch my web application
However I cant seem to switch onto this pop up as if its a window
new WebDriverWait(driver,5).until(ExpectedConditions.numberOfWindowsToBe(2));
Set<String> allHandles = driver.getWindowHandles();
for(String winHandle:allHandles)
{
if (!first_handle.equalsIgnoreCase(winHandle))
{
driver.switchTo().window(winHandle);
}
}
And I also attempted to accept it as an alert, but it didnt recognise it as an alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
Ive seen suggestions to disable notifications and they dont work either, my main aim is to select the open button but I cant get any elements to select from the console either
You may need to disable notifications for a selenium web driver at a WebDriver level by adding options. Here is an example of how to do it for ChromeDriver:
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "/home/users/user.user/Desktop/softwares/chromedriver");
WebDriver driver =new ChromeDriver(options);
driver.get("http://your.url/");
driver.manage().window().maximize();
I am writing a code for Facebook where it takes the URL, ID, Password from a properties file but upon logging in I am hinted with a "Facebook wants to show notifications - Allow - Block" How do I make it so after login it (A.) Presses ESP or ALT+F4 and closes the popup or (B.) Finds the notification and closes it itself. This is what Im using but its not working. Any help is appreciated.
public void closePopup() throws InterruptedException{
Thread.sleep(1000);
Actions action=new Actions(driver);
action.keyDown(Keys.ESCAPE).keyUp(Keys.ESCAPE).build().perform();
After further research I found my answer. It is a chrome notification so here is the required step to solve my problem.
ChromeOptions ops = new ChromeOptions();
ops.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "./lib/chromedriver");
driver = new ChromeDriver(ops);
Please Follow below steps :
Step 1:
//Create a instance of ChromeOptions class
ChromeOptions options = new ChromeOptions();
Step 2:
//Add chrome switch to disable notification - "--disable-notifications"
options.addArguments("--disable-notifications");
Step 3:
//Set path for driver exe
System.setProperty("webdriver.chrome.driver","path/to/driver/exe");
Step 4 :
//Pass ChromeOptions instance to ChromeDriver Constructor
WebDriver driver =new ChromeDriver(options);
I have written the following code to handle a native OS pop up that is raised after clicking on a verification link in a webpage. It almost works, except that after the key presses are sent away to the pop up and next page is loaded I don't have control over my driver anymore. No Selenium code I have tried after, seems to be able to work properly. Either a WebDriverException is raised (from some internal Selenium Javascript file) or a time out Exception is raised. I've experimented with getting the page handle and trying to switch back to it, explicitly wait with FluentWait until an element in the new page is present and have tried different Selenium code after with no avail. Any cues to understand the problem here is much appreciated.
Here is my Java code snippet with some inline comments:
driver = new FirefoxDriver();
// String windowHandle = driver.getWindowHandle(); //Saving the window handle to switch to it later
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.navigate().to("https://mysite/login"); //Website with native OS pop up for authentication
driver.findElement(By.cssSelector(".loginbox")).click();
//Convenience method to send a series of key codes to java.awt.Robot
sendKeySequence(KeyEvent.VK_TAB, KeyEvent.VK_TAB, KeyEvent.VK_ENTER);
//TRYING TO GET HOLD OF THE WEB ELEMENTS AGAIN: Nothing works!
//1st try: fluentWait(By.id("BigBox")).click(); //Method to wait until WebDriver element is present on the page
//2nd try: driver.switchTo().window(windowHandle);
//driver.get("www.google.com");
sendKeySequence Implementation:
private void sendKeySequence (int... commandStream) {
Robot robot = new Robot();
for (int keyCode : commandStream) {
robot.keyPress(keyCode);
robot.keyRelease(keyCode);
}
}
PS! For the fluentWait implementation refer to this post
Try using the robot like this (Assuming* you need to press 2 TAB keys and 1 Enter Key using Robot class):
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
Then, try waiting for the new element in the page, using the below code:
try{
WebDriverWait wait = new WebDriverWait(driver,30);
WebElement element = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//xpath of the element")));
element.click();
}catch(Throwable e){
System.err.println("Error came while waiting and clicking the element. "+e.getMessage());
}