Unable to automate the Popup in selenium - selenium-webdriver

i am unable to identify the web elements when a pop up is displayed on clicking "select your family members" in the below website, i am not sure how to automate this ?
I have tried using Switch window and alert windows.
http://health.policybazaar.com/?utm_content=home_v3

There is no need to switch any window , Use following code
driver.get("http://health.policybazaar.com/?utm_content=home_v3");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
driver.findElement(By.id("input_6")).click();
Updated :
WebDriverWait wait =new WebDriverWait(driver, 120);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[#class='prequote-member-left-section']/md-checkbox[#name='checkboxSelf']/div[1]")));
driver.findElement(By.xpath("//div[#class='prequote-member-left-section']/md-checkbox[#name='checkboxSelf']/div[1]")).click();
Note : Use ExplicitWait to make element visible on next popup and then click

Related

Handling print window in selenium webdriver

There is a print button in my application. When I click on the button print windows open with cancel and print button.
I want to click on cancel button in the print window.
I know handling print window is not possible using selenium webdriver alone. I tried using Robot class. But it is not working.
WebDriver driver = (WebDriver) new ChromeDriver();
driver.get("https://www.joecolantonio.com/SeleniumTestPage.html");
driver.manage().window().maximize();
Thread.sleep(2000);
//clicking on the print button
driver.findElement(By.id("printButton")).click();
//print window opens
//creating robot class object
Robot r = new Robot();
r.delay(1000);
r.keyPress(KeyEvent.VK_ESCAPE);
r.keyRelease(KeyEvent.VK_ESCAPE);
The print window opens but nothing happens after that. I was expecting escape button will make the window exit but its does not happens. But manually when I press escape button print window disappears.
Actually you can handle print page window using javascript executor in selenium.
following codes are c#.
If your program is stuck after opening window.print(); which in my case it was happening, try to use the following javascript code instead:
setTimeout(function() { window.print(); }, 0);
this will release your application and you can work with the print page.
then you can switch to the print page simply by the following line:
driver.SwitchTo().Window(driver.WindowHandles.Last());
then you can get the JSPath of any element in print window and interact with it
(IWebElement)js.ExecuteScript($"return {JSPath}").Click();
A complete example of this, is as follows where print window will be opened then the cancel button will be clicked after 2 seconds.
IJavaScriptExecutor js = (IJavaScriptExecutor)driver;
js.ExecuteScript("setTimeout(function() { window.print(); }, 0);");
driver.SwitchTo().Window(driver.WindowHandles.Last());
Thread.Sleep(2000);
string JSPath = "document.querySelector('body>print-preview-app').shadowRoot.querySelector('#sidebar').shadowRoot.querySelector('print-preview-button-strip').shadowRoot.querySelector('cr-button.cancel-button')";
IWebElement cancelBtn = (IWebElement)js.ExecuteScript($"return {JSPath}");
cancelBtn.Click();
Since the elements in print window exist in the shadow root you cannot select them by selenium web driver that's why I used Javascript Executor to get the cancel button.
Use tab and then click escape.I think it works.
Using Robot is not something I would recommend to handling the print preview page as it will bite you back when you will be running your tests in Parallel mode.
You could click "Cancel" button as follows:
Switch to the Print Preview window using driver.switchTo() function:
driver.switchTo().window(driver.getWindowHandles().stream().filter(handle -> !handle.equals(driver.getWindowHandle())).findAny().get());
Find preview-app Shadow DOM element using i.e. XPath and convert it into a WebElement
WebElement printPreviewApp = driver.findElement(By.xpath("//print-preview-app"));
WebElement printPreviewAppContent = (WebElement) driver.executeScript("return arguments[0].shadowRoot", printPreviewApp);
Find preview-header Shadow DOM element using i.e. CSS and convert into a Selenium's Web Element
WebElement printPreviewHeader = printPreviewAppContent.findElement(By.cssSelector("print-preview-header"));
WebElement printPreviewHeaderContent = (WebElement) driver.executeScript("return arguments[0].shadowRoot", printPreviewHeader);
Locate Cancel button and click it
printPreviewHeaderContent.findElement(By.cssSelector("paper-button[class*=cancel]")).click();
Answer for someone else who still looking solution
To download save as a pdf from the print pop-up window from any web
Add argument
options.addArguments("kiosk-printing");
This will prevent to open any popups from clicking on any print button.But a window dialog folder will open to download the file as a pdf.
Now you need to input the file name and press enter button. For this, we will use the robot class
StringSelection s=new StringSelection("remotePetrol_report.pdf"); // Set the File Name
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(s,null);
//native keystrokes for CTRL, V, and ENTER keys
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);

Selenium Web-Driver Popup

I am unable to write on this popup message using selenium.
Please feel free to help me in this case.
My Code is:-
public static void main(String[] args)
{
System.setProperty("webdriver.gecko.driver", "F:\\gecko_driver\\geckodriver-
v0.16.1-win64\\geckodriver.exe");
WebDriver driver= new FirefoxDriver();
driver.get("https://www.heycare.com");
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
System.out.println("hello world-----1");
driver.switchTo().frame(0);
driver.findElement(By.id("mobile")).sendKeys("7015273543");
System.out.println("hello world-----2");
//Driver.findElement(By.id("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
}
Error:-
Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: *[name='mobile']
Code is working fine till:-
driver.findElement(By.xpath("html/body/div[2]/header/div/nav/div/a")).click();
Unable to write on that popup form that comes after clicking on the login button..
I navigated to that website and successfully targetted the mobile number field using this css selector #mobile which targets the id attribute. You're doing this for your password field. Try having your test use this:
Driver.findElement(By.id("mobile")).sendKeys("7015273543");
See if that helps...
Edit: I'm not sure that the unformatted code block you're showing that switches frames is necessary. Unless you're working with iframes, I didn't see any in the minute I was looking around.
Edit2:
WebDriver driver = new FirefoxDriver();
driver.get("https://www.heycare.com");
System.out.println("hello world");
driver.findElement(By.className("log-pop")).click();
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.name("mobile")));
driver.findElement(By.name("mobile")).sendKeys("7015273543");
driver.findElement(By.id("Pass")).sendKeys("123456");
You could try something like this, I added in a call to WebDriverWait to account for the possible delay between clicking the login button, and waiting for the login prompt popup to finish it's animation prior to be allowed to be clicked. I'm not sure if this will work in your case, but it's a possibility I sniffed out when I was observing the site's behavior.
(I didn't test any of this code, it's freehand written. So take it with a grain of salt. It might not work flawlessly as is.
Bit of java nit, you should make your object references lowercase, save capitalized symbols for class names. I had a moment of trying to figure out why you were calling static methods of a class called Driver instead of an instance of the class WebDriver

Selenium webdriver automation, automating context menu options

I am not able to click on any element after doing right click in selenium(Java).
Its just doing a right click and is not clicking of any of the options like open in new tab...Instead its just doing a normal click after doing a right click.Can anyone please help me.Below is my code
System.setProperty("webdriver.chrome.driver","C:\Selenium\chromedriver.exe");
WebDriver wd=new ChromeDriver();
wd.get("http://google.com");
Thread.sleep(3000);
//Point a=wd.findElement(By.linkText("Testing")).getLocation();
WebElement b=wd.findElement(By.linkText("About"));
Actions action=new Actions(wd);
//action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ENTER).perform();
action.moveToElement(b);
Thread.sleep(4000);
//action.contextClick(b);
action.contextClick(b);
action.sendKeys(Keys.ARROW_DOWN).sendKe ys(Keys.ENTER).build().perform();
I have tried via context click and move to element as well but no result.Thanks in advance..
Might be this is what you want:
To select the item from the contextual menu, you have to just move your mouse positions with the use of Key down event like this:-
Actions action= new Actions(driver);
action.contextClick(b).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.ARROW_DOWN).sendKeys(Keys.RETURN).build().perform();
Soure: Select an Option from the Right-Click Menu in Selenium Webdriver - Java
As per request in the comments adding alternative way to open a link in new tab.
System.setProperty("webdriver.chrome.driver", "Drivers\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.get("http://www.google.com/");
WebDriverWait wait = new WebDriverWait(driver, 30);
Thread.sleep(3000);
WebElement b=driver.findElement(By.linkText("About"));
Actions action=new Actions(driver);
action.moveToElement(b).perform();
Thread.sleep(4000);
action.keyDown(Keys.CONTROL);
action.click();
action.keyDown(Keys.CONTROL).build().perform();
//action.sendKeys(Keys.RETURN).perform();
Thread.sleep(4000);
driver.quit();
The build() method is used compile all the listed actions into a single step. We use build() when we are performing sequence of operations. We can directly use perform() if we are performing single action. You can read more about Actions class.
Also, re-iterating that I was unable to find the root of the issue in the limited time I had, so I placed a work around. So like we use the shortcut CTRL+CLICK to open the link in new tab manually, You might need to find out the shortcut for what you need, you can refer this : https://support.google.com/chrome/answer/157179?hl=en
Hope it helps :)

selenium IDE is successfully sending keys to hidden input box, but webdriver is throwing an error as element not visible

I have an input box which is not visible, Selenium IDE in record and playback mode is able to send keys into it and out put is successful.
same thing webdriver is throwing an error element is not visible therefore cannot be interacted with.
I have tried scripting using document.findElements.ByclassName.. there is no error but there is no output as well.
pls see the code below:
{
driver.findElement(By.cssSelector("li.cwd-clue")).click();
assertTrue(isElementPresent(By.xpath("//input[#class='cwd_input']")));
System.out.println("assert true");
WebElement tmpElement= driver.findElement(By.className("cwd_input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementsByClassName('cwd_input') [0].click();",tmpElement);
tmpElement.sendKeys("TELLER");}
Add WebDriverWait as shown.
WebElement ele=driver.findElement(By.xpath("//input[#class='cwd_input']");
WebDriverWait wait=new WebDriverWait(driver,10).until(ExpectedConditions.presenceOfElementLocated(ele));

How can I check a checkbox on the popup window? Selenium Webdriver

When a popup window open how can I handle on this window?
driver.switchTo().defaultContent();
MenuUtil.doClickMenu(selenium, driver, "Access 2G", "Cảnh báo", "Giám sát cảnh báo BTS");
ControlBase.waitForLoadOk(5);
CheckBoxUtil.doCheckByXpath(selenium, "//*[#id='listRow1']/tbody/tr[1]/td[18]/input");
ControlBase.waitForLoadOk(5);
ButtonUtil.doClickByLabel(driver, "Cập nhật");
ControlBase.waitForLoadOk(5);
CheckBoxUtil.doCheckByXpath(selenium, "//*[#id='txtName']");
Generally I use the Selenium IDE. Here you can 'click & record', which allows you to then get the XPath of the JS pop-up alert box.
I would suggest you first check for the alert box showing up with something such as this:
if (selenium.isAlertPresent()){
System.out.println(selenium.getAlert()); //show alert text message
selenium.click("id=alertbox_ok_btn"); //click OK on alert box
}
Download Selenium IDE from here: http://docs.seleniumhq.org/download/
Also, refer to Selenium's documentation for Java here: http://selenium.googlecode.com/svn/trunk/docs/api/java/index.html
Try the below code written in JAVA
WebDriver driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.get("Your application URL");
String parentWindow = driver.getWindowHandle();
//click on the link/box/whatever to open the pop up
driver.findElement(By.xpath("")).click();
Set <String> popupWindows = driver.getWindowHandles();
for(String popup : popupWindows){
driver.switchTo().window(popup);
//you can use getTitle/getCurrentUrl to match with title/url of the pop up
if(driver.getTitle().equals("actual title on the pop up")){
break;
}
}
//Write a code to check a checkbox
//Close the pop up
driver.close();
//Come back to the parent window
driver.switchTo().window(parentWindow);
//Code if you want to test/do something else
driver.close();

Resources