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();
Related
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 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");
After allowing for "all the site to get your physical location" using chrome setting, still popup getting populated. How to handle geo location popup using java selenium? Can anyone please help me?
To disable this message you need to add chrome options:
options.addArguments("--enable-strict-powerful-feature-restrictions");
options.addArguments("--disable-geolocation");
Full example:
public WebDriver createDriver(){
ChromeOptions options = new ChromeOptions();
options.addArguments("disable-infobars");
options.addArguments("disable-notifications");
options.addArguments("--enable-strict-powerful-feature-restrictions");
options.addArguments("--disable-geolocation");
return new ChromeDriver(options);
}
When recording in selenium IDE I can click the "OK" button in a popup, and expected to be able to click it using
driver.findElement(By.linkText("OK")).click();
but this was not the case.
Similarly this doesn't work.
driver.switchTo().alert().accept();
Selenium throws a NoAlertPresent exception. If what's popping up is not an alert, then what is it? And how do I click yes!
in such case I'd prefer to check(verify) the alert presence on the page and then if is present - accept it.
It be somthing like:
public boolean isAlertPresent() {
boolean presentFlag = false;
try {
// Check the presence of alert
Alert alert = driver.switchTo().alert();
// Alert present; set the flag
presentFlag = true;
// if present consume the alert
alert.accept();
} catch (NoAlertPresentException ex) {
// Alert not present
ex.printStackTrace();
}
return presentFlag;
}
here you can get details
Also do not forget about debug step by step.
Hope this helps you.
It could be anything. You should be telling us that.
If it is a Java Script alert then, this should work
driver.switchTo().alert().accept();
At the very least you could try sending enter/return key stroke, if the "OK" button is autoselected/highlighted by the web app.
import org.openqa.selenium.Keys
WebElement.sendKeys(Keys.RETURN);
Update
It could also be because your alert is not present at the time you are trying to click/accept it.
For a quick check put in a sleep of 4-5 seconds and then try driver.switchTo().alert().accept();. Once it is ascertained, then put in a wait for alert present in a try and catch loop (any exception handling).
if you are using latest version of webdriver, infact anything above 2.20 then
driver.switchTo().alert().accept();
should work provided the alert is a javascript alert similar to the one we get when we click
alert demo OR confirm pop-up demo
Updated
here this code will help you accept the alert
driver = new FirefoxDriver();
String baseUrl = "http://www.w3schools.com/js/tryit.asp?filename=tryjs_alert";
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get(baseUrl);
driver.switchTo().frame(0);
driver.findElement(By.cssSelector("input[type=\"button\"]")).click();
driver.switchTo().alert().accept();
When I use Selenium 2 code (Java) to open Firefox (or any other browser) for some automated tests, the new window opens without my bookmarks, or for that matter the bookmark bar. Additionally, I suspect that cookies aren't retrieved either, because sites I normally log into do not remember certain things from my previous history.
The relevant code:
//WebDriver driver = new FirefoxDriver();
WebDriver driver = new InternetExplorerDriver();
String baseUrl = "http://localhost:8080/";
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
//Navigate to login page
driver.navigate().to(baseUrl + "/myApp");
//obtain the username and password elements
WebElement username = driver.findElement(By.name("username"));
WebElement password = driver.findElement(By.name("password"));
//log in
username.sendKeys("myTestLogin");
password.sendKeys("myTestPwd");
driver.findElement(By.cssSelector("input.btnStyle")).click();
...
I think by default Selenium (WebDriver) will try to use as "clean" of a profile as possible. This is so the browser's settings that a user set up don't cause testing failures. You can modify these settings if you need to. Check out http://code.google.com/p/selenium/wiki/TipsAndTricks and see if that helps get you on the right track. I haven't done this with IE before though. I think with Firefox you can even have Selenium use an existing profile if you really need it to.