How to switch focus to browser popup using selenium webdriver c# - selenium-webdriver

I need to accept a web browser popup which comes on refreshing the website.
I have used following script to refresh the web site -
driver.FindElement(By.Id("all_bt")).SendKeys(Keys.F5);
After that I used below code to accept the web browser popup.
IAlert alert = driver.SwitchTo().Alert();
Thread.Sleep(3000);
alert.Accept();
But I am getting error as Alert not found.
I believe the focus is not on the popup, that may be the reason.
Can anybody help me on this?

If you need to refresh the page, instead of sendkey with F5 you can try as below in Java
driver.navigate().refresh();
Regrading popup, are able to inspect elements in that popup? if you are able to inspect popup then it will be normal page not Alert.
Cross check the locator used, try with required wait to appear that ok button and is that button inside any frame or not (iframe). if it is inside frame then switch frame before click. After work is done in frame then switch back to default content.
driver.switchTo().frame("provide frame name or location"); //switch to frame command
driver.switchTo().defaultContent(); //switch to default content command
//below is wait command
WebDriverWait wait=new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.presenceOfElementLocated(By.id("id of ok")));
Thank You,
Murali G

Try directly click on ok .. if still not work then check if there is any frame present .. it must a application pop-up besides browser pop-up .. switch to alert won't work in that case
Hope it will help you :)

Related

How to remove login pop up in selenium

I am trying to login to Flipkart and make a purchase. But as soon as i click on the Login/SignUp button, a pop up comes suggesting existing usernames and has a Cancel button.
I am unable to find code for Cancel button through Inspect. How can i handle this?
To interact with the new window you have to change the element with which Selenium interacts, you can use the switchTo method and then indicate the name of the window:
driver.switchTo().window("windowName");
If you do not know the name of that popup that appears to you, you can get all the windows and look for the cancellation button:
for (String window : driver.getWindowHandles()) {
driver.switchTo().window(window);
List<WebElement> elements = driver.findElements(By.id("cancel"))
if (!elements.isEmpty()) {
elements.get(0).click();
break;
}
}

Not able to handle popup window that is showing immediately after login

After login my main window disabled and one popup window showing. I tried to switch window, directly clicking cancel element on alert but none of them working.
I attached image with html code and popup window for your reference.
As per screenshot, cssSelector having 2 matching nodes, so have a look for specific Cancel element. It may pointing to wrong element.
If you have correct locator then try with
waits, if not works Thread.sleep helps sometime to simulate click properly,
As HTMLcode is not clear, check if Cancel element is inside iframe/frame or not,if it is inside a iframe/frame then switch to frame,
As we are seeing HTML code, so it is not Alert, check in HTML instead of Firepath to see if it is new windows or try getWinddowHandles, get size of set, to see as it is window or not
Set<String> handles = driver.getWindowHandles() ;
We can try, switch to active element also
If webdriver click is working, you can try click by using javascriptexecutor
WebElement element1 = driver.findElement(By.xpath("//elementpath"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", element1);

How can I click on a popup window in selenium Java

I am new to selenium and try to automate (using selenium Webdriver in Java for Window and using Chrome Driver) an App (ABC Project) which contains a Registration Form.
After completing the form and click on Registration Button I get a popup info message (modal) with close (X) & OK Button & Headline in the message: Under ABC Project the following is displayed & Text which informs the user that the registration was successful.
I have tried several ways to click on OK button in this popup window but no success:
1.
Alert alert = driver.switchTo().alert();
alert.accept();
2.
driver.findElement(By.xpath("//input[#value = 'alert']")).click();
Alert javascriptAlert = myTestDriver.switchTo().alert();
System.out.println(javascriptAlert.getText()); // Get text on alert box
javascriptAlert.accept();
----> in this case I get only the text of the opened tab (from registration window: driver.getTitle();:ABC Project) but not the text of teh Info message(to see in logger.info)
3.
String winHandleBefore =
driver.getWindowHandle();
driver.findElement(By.xpath("//div[#class='col-md-4']/button[1]")).click();
// Xpath of register Button
Set handles = driver.getWindowHandles(); ..... --->>, In this case, I don't get any Window Handler of new window the window handler from old and new are the same
Additional Hint:
I am also not able to click anywhere else in Window to skip the popup window
In Google Developer tool I don't see also any source code and no Elements from the popup window (when the popups appear.
I heard from developer that this window is a javascript but I don't
see anything in source code too (it did not also work with solution b
above)
I appreciate for any tips and supports
Thanks
This picture posted looks like a javascript alert.
So the below code should have worked.
Alert alert = driver.switchTo().alert();
alert.accept();
Maybe it is a wait issue. Try WebDriverWait
Alert alert = new WebDriverWait(driver, 20).until(ExpectedConditions.alertIsPresent());
alert.accept();

accessing invisible popup in selenium webdriver?

in a interview they have asked below questions please tell me code or approach ...
1-how to access invisible popup.in selenium alert pop can be accessible using alert.accept() but if alert popup is invisible then how can we use.
2-code for automatic image display slider,some image slider has next and previous button we have to click next or previous so please let me know how to develop it in both way by clicking next button on image or automatic image display slider
i am using selenium web driver,java,Firefox
thanks a lot
For the 1st question, AssertEquals will help us to go through invisible pop-ups and for the second one, I am not sure that they have asked the slider next or previous button will be on image....if it is outside image, then we can verify that if its present than click on particular id else not.

Celerity - Help with popup_browser select list that updates div text on main browser

I'm screen scraping a page that includes a link that spawns a popup window that is comprised of a select list and a button to execute your selection. The objective is to click the link on the main page, make a selection on the pop-up window, click the link to confirm the selection and then view the new selection on the main webpage.
Here are the steps that I'm taking in Celerity and the results:
popup_browser = browser.image(:alt, 'Holidays').click_and_attach
#this creates a new browser to deal with the popup window
popup_browser.select_list(:id, 'ddlSlot1').select_value('Christmas')
#Selecting Christmas from the select list
popup_browser.link(:id, 'btnChangeHoliday').click
#Confirms/Implements selection
popup_browser.close
#Closes popup browser
puts browser.div(:id, 'HolidayName').text
#Here I try to print the updated holiday but nothing is printed (no text value)
Everything seems to work fine except for the last line. I've done some debugging and have confirmed that my selection has been made. It seems that the main webpage is not being updated after I click the ChangeHoliday button on the popup page.
Any thoughts or suggestions would be appreciated.
Thanks in advance for you help.
This turned out to be an easy fix. The only change required for the script to work is to add the resynchronize option to the browser initialization. After doing so the script works as expected.
browser = Celerity::IE.new(:resynchronize => true)

Resources