Closing an Alert with Selenium Webdriver - java

Unable to close an alert which pop-ups once the url is navigated and enter something in the page(https://www.roadrunnersports.com).
I tried using sendkeys and using actions class aswell.
Manually, I am able to close the alert but I want to automate that aswell.
Anyone suggest me a way to handle this?
Note:Its not a sweet alert also to as I am unable to inspect the element.

It looks like it isn't consistently the same modal. But, the ones I've seen can be cleared by hitting the Esc key. Try using that with actions
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
actions = ActionChains(driver)
actions.send_keys(Keys.ESCAPE).perform()
Make sure you wait for the modal to appear though, it seems like it takes a bit.

Actions actions = new Actions(driver); // define action class
actions.sendKeys(Keys.ESCAPE).build().perform(); // Press ESC key

Related

moveToElement mouse hovering function in Selenium WebDriver using Java not stable

I'm using Selenium 3.0.1 for running automation tests using TestNG.
In one test I'm trying to hover on an action menu and then click an option in that menu:
Actions builder = new Actions(getWebDriver());
builder.moveToElement(actionButton).build().perform();
But the test is not stable. I can see the menu opens but immediately closing, so the test fails because it's not finding the option any more.
I'm receiving this error:
java.lang.IllegalArgumentException: Must provide a location for a move action.
at org.openqa.selenium.interactions.MoveMouseAction.<init>(MoveMouseAction.java:30)
at org.openqa.selenium.interactions.Actions.moveToElement(Actions.java:251)
How can I check if the menu is open? the perform() method is returning void.
I notice if I put call the moveToElement twice, than the test is being more stable. Is there any elegant option of doing so?
Actions builder = new Actions(getWebDriver());
builder.moveToElement(actionButton).build().perform();
builder.moveToElement(actionButton).build().perform();
This how the menu looks like when we hover over it:
I find this issue:
https://sqa.stackexchange.com/questions/3467/issue-with-losing-focus-of-hover-command-when-the-mouse-is-outside-of-the-acti
which explains best my problem. unfortunately, still with no solution.
If it is not necessary for you to open the menu, please try clicking the option using JavascriptExecutor. JavascriptExecutor can click a hidden element as well, all that is necessary for the click to be triggered using JavascriptExecutor is that the element is present on the DOM.
Snippet (Java):
((JavascriptExecutor)driver).executeScript("arguments[0].click()", driver.findElement(By.cssSelector("hiddenOptionFromMenu")));
You can wait for the menu to appear after the hover with a FluentWait, like so:
FluentWait<> wait = new FluentWait<>(getWebDriver())
.withTimeout(driverTimeoutSeconds, TimeUnit.SECONDS)
.pollingEvery(500, TimeUnit.MILLISECONDS)
.ignoring(StaleElementReferenceException.class)
.ignoring(NoSuchElementException.class)
.ignoring(ElementNotVisibleException.class)
wait.until(x -> { return driver.findElement(menuElementBy); } );
If the mouse hover succeeded - the menu starts appearing - there's no reason you need to call it twice.
It seems like a timing issue.
If the menu has a transition effect, then add a delay of the duration of the effect:
new Actions(driver)
.moveToElement(menu)
.pause(100) // duration of the transition effect in ms
.perform();
submenu.click();
You could also wait for the targeted element to become visible and steady (same position returned twice in a row).

Selenium Webdriver : Dismiss Chrome Alert

I want to select the check box Prevent this page from creating additional dialogs before selecting ok to close the alert
Currently i am using
Alert alert=driver.switchTo().alert();
//check the checkbox
alert.accept();
As an normal interactive user to check the check box i have to use a combination of <Tab> + <Space/Enter> keys. The <Tab> shifts the focus to the check-box and the <Space/Enter> checks the check-box.
Solutions Tried
I tried using Java sendKeys mechanisms ( Robot class, driver.sendKeys(), etc.), but an UnhandledAlertException is getting thrown.
I tried using alert.sendKeys which is different from driver.sendKeys() but it too failed
//check the checkbox
alert.sendKeys("\t");
alert.sendKeys("{TAB}");
alert.sendKeys("\uE004");
alert.sendKeys("\\U+0009");
alert.sendKeys(Integer.toString(KeyEvent.VK_TAB));
I am trying to avoid robot class as much as possible as i need to run the test in grid in which case robot class will not work.Any pointers on how to send keys to the alert window ?
Temporarily i could do it using a javascript window.alert = function() {}; by simply overriding the alert with an empty function just curious to know if it could be done with webdriver functions like alert.sendkeys or any other methods ??
Any help is greatly appreciated!!

Handle Window Pop Up in Selenium

I am working with Selenium, now there is a condition:
when I hit a button in my webpage a window pop up opens up.
Now I have to click a radio button (one out of two, it will work even if we send a TAB ) and then click an OK button. I searched in the net and got to know about "driver.getWindowHandle()".
But I don't have any idea dealing with the newly opened window popup.
Need help in this.
For switching purpose u can use enhanced for loop:
for (String winHandle : objDriver.getWindowHandles()) {
objDriver.switchTo().window(winHandle);
}
So it will switch the control from one driver window to child windows.
To interact with elements on the window try to find element with whatever tool u r using and perform the required action after switching to the window.
To return back to parent window you can use the same loop or use:
driver.switchTo().defaultContent();
Check my answer in this post and also read the comments to help you understand the difference between getWindowHandle() and getWindowHandles()
Java: focus is not on pop-window during window handling
We handled this situation using AutoItX - https://www.autoitscript.com/site/ in our Windows/IE C# project:
AutoItX3 autoIt = new AutoItX3();
var handle = autoIt.WinWaitActive("[window title]", "", 20);
Assert.IsTrue(handle != 0", string.Format("Was not able to find: {0}", [window title]);
autoIt.Send("{ESCAPE}"); // tab may work as well for selection
The pop up was a Windows window, and not part of IE, therefore the WebDriver didn't know about it.
Hope this helps.

Webdriver test doesn't select option from dropdown

i have a registration form for checkout,I have to select a state from drop down,for which i have written a script:
WebElement wb = driver.findElement(By.name("user_data[s_state]")) ;
Select selwb = new Select(wb) ;
selwb.selectByValue("KR");
driver.findElement(By.name("dispatch[checkout.update_steps]")).click() ;
but after executing this script,it is not selecting given value from dropdown.hence i am unable to proceed on next step. Plz help me out....
I can give you a suggestion since I have worked with Selenium webdriver for sometime. The webdriver executes very fastly. Selenium driver doesnt take care whether the page loaded or not. So I recommend you to add code just before the click/selection events to make sure that the page loaded completely. There are options like waitForPageLoad() or checkifComponent exist to make sure that page is loaded properly before the events are triggered. Hope this will help you.
If any wait doesn't help - like suggested before - try to select eg. by index or text.
I've had such wired cases where one of ByValue/ByText/ByIndex method didn't work although others did with particular dropdown.
If you are about to select the drop down use the following:
First make the webdriver wait
Then try choosing the element with the help of
driver.findElement(By.xpath("xpath of the value").select
OR
you can use like the following:
new WebDriverWait(driver,30).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("the xpath of the value"))).click();

Getting Java Webdriver 'Down Arrow' action to work

So I have been trying to resolve this for several hours. I have no clue what I am doing wrong.
This is a type ahead field I am looking in is <input type="text" id="id_attendees" name="attendees">. When I type in there a js dropdown is created. When I press the Down Arrow on keyboard it works fine and selects the top choice. When I do keyDown --- id=id_attendees --- \40 in IDE it works fine and also selects the choice.
I cannot get it to do the same in Java webdriver though
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN);
^doesn't work.
driver.findElement(By.id("id_attendees")).sendKeys(Keys.ARROW_DOWN);
^doesn't work
I tried Keys.DOWN in both cases, that doesn't work either. I created a literal String altm = "\u0040"; and all that does is type an # symbol.
I also tried a bunch of other things as well and nothing is working. I have no clue what am I missing.
EDIT 1:
#Roddy Thank you! - Given that link I added the following that did work (after importing DefaultSelenium and WebDriverBackedSelenium.
DefaultSelenium sel = new WebDriverBackedSelenium(driver,vars.siteurl);
sel.fireEvent("//input[#id='id_attendees']", "keydown");
EDIT 2:
--> DOH that doesn't work. I got overzealous apparently.
some time scripts takes some time to load the list so need to add wait,
WebElement ar=driver.findElement(By.id("id_attendees"));
Thread.sleep(1000);
ar.sendKeys(Keys.ARROW_DOWN);
I think your use of Actions is not quite right.
The implementation is a builder pattern. Calling sendKeys doesn't send the event, it only stages the event to be fired when you call perform. Note that the return value of sendKeys is an Actions instance.
Actions actionObject = new Actions(driver);
actionObject = actionObject.sendKeys(Keys.ARROW_DOWN); //ASSIGN the return or you lose this event.
actionObject.perform(); //Should do what you want. Note that this will reset the builder.
Hope that helps.
With Actions class, after defining what it will do for you, you need to first build() it. So in your case it would be like this:
Actions actionObject = new Actions(driver);
actionObject.sendKeys(Keys.ARROW_DOWN).build();
When you want your script to execute that action, you need to perform() it. You can chain it right after your build() method (if you are using it just once, for example) or later in your code whenever you need it, like this:
actionObject.sendKeys(Keys.ARROW_DOWN).build().perform();
OR
actionObject.perform();
Good luck!

Categories