I am trying to handle popup using phantomjs as a driver and I want to copy the text of alert/popup in variable.
I write the code:
But I am getting exception:Exception in thread "main" java.lang.NullPointerException
Anyone knows how to handle popup/alert using phantomjs with webdriver.
I write the code:
js.executeScript("window.alert = function(msg){JavascriptExecutor js=(JavascriptExecutor) driver;
document.lastAlert=msg;};");
Object text = js.executeScript("return document.lastAlert");
System.out.println(text.toString());
Selenium has methods to interact with javascript alerts. You can interact with a javascript alert as follows:
Alert alert = driver.switchTo().alert();
From here on you can get the alert text with:
String alertText = alert.getText();
You do not need any javascript to do this. Just plain Java code does all that for you.
Related
We are working on IE Automation using Selenium Web driver in C#.Net.
We are getting an exception in handling model popup window. We supposed to do below the action.
When we click on Link button it will open a popup window then we need switch to popup window selecting check box options and click on Submit button.
When clicking on Link button we are able to open the popup window. But here we are facing an issue like the child popup window is not loading with data and getting HTTP 500 Internal server Error.
I don't understand sometimes it was working properly with the same code but not all the times I am getting above issue when I am trying to perform above actions on child window.
is this any IE settings issue or my code issue even i ignored protected mode settings in IE settings.
I am trying with below code :
js.ExecuteScript("arguments[0].click();", driver.FindElement(By.XPath("//*[#id='ByNewNotes']")));
(or)
string jsWindowString = "NewWindow('pop_Type.jsp?Type=External&IuserId=NUVJK50'," + sessionId + ",'400','500');return false";
((IJavaScriptExecutor)driver).ExecuteScript(jsWindowString);
Could you please help on this issue.
Thanks in Advance.
Instead of using ExpectedConditions.ElementExists use ExpectedConditions.elementToBeClickable or presenceOfElementLocated
WebDriverWait wait = new WebDriverWait(driver, 60);
WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""//*[#id='ByNewNotes']")));
element.click();
Either Try to use FluentWait. Create a by function of your element which you want to wait and pass it in below method
WebElement waitsss(WebDriver driver, By elementIdentifier){
Wait<WebDriver> wait =
new FluentWait<WebDriver>(driver).withTimeout(60, TimeUnit.SECONDS) .pollingEvery(1, TimeUnit.SECONDS).ignoring(NoSuchElementException.class);
return wait.until(new Function<WebDriver, WebElement>()
{
public WebElement apply(WebDriver driver) {
return driver.findElement(elementIdentifier);
}});
}
Hope it will help you :)
Have you tried
Thread.Sleep(2000);
We had the same issues and solved it with this simple way.
I'm automating an application using Selenium so what is happening is when the script runs and it hits the login button an error comes up in the top right which stops further execution. What I want is need a way by which i can capture this "error message" so that I would know the cause of the halt of the application execution.I'm attaching the image of error and also the code that I have tried but it didnt work.
#FindBy(xpath="//*[#id='toast-container']/div/div]")
static WebElement errorMessage;
WebDriverWait wait = new WebDriverWait(CommonBrowserSetup.driver, 15);
wait.until(ExpectedConditions.visibilityOf(errorMessage));
String mess=errorMessage.getText();
System.out.println(mess);
If you want to capture Unknown error occured
Just try the following code and let me know if it works for you.
After the login button is clicked add the following:
WebElement Error = driver.findElement(By.xpath("//div[contains(text(),'Unknown error occured')]"));
if(Error.isDisplayed()){
String Errormessage = Error.getText();
System.out.println(Errormessage);
}
I have the java script injection to send text into a hidden inputbox. but webdriver is throwing an error as element is not visible so cannot be interacted with.
WebElement tmpElement= driver.findElement(By.className("cwd_input"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript(("document.getElementsByClassName('cwd_input')
[0].click();"),tmpElement);
tmpElement.sendKeys("T");
you can set value of the input via JS.
Code will be something like:
string scriptToExecute = "document.getElementById("mytext").value = 'New value';";
executor.executeScript(scriptToExecute);
I have written an application in selenium using JAVA to login into website. Following is my code
import org.openqa.selenium.JavascriptExecutor;
WebDriver driver = new FirefoxDriver();
driver.get("http://inernalportal.com");
WebElement element = null;
element = driver.findElement(By.id("txtLoginID"));
element.sendKeys("user");
element = driver.findElement(By.id("txtpassID"));
element.sendKeys("password");
element = driver.findElement(By.id("btnLogin"));
element.click();
However above code works correctly but there is one issue, while filling password by Seleinum driver user could click in address bar to see password.
Is there any way to prevent left/right mouse click in Selenium?
Your help is highly appreciable.
That should resolve problem :)
((JavascriptExecutor) driver).executeScript("document.getElementById(\"txtpassID\").value = \"password\";");
I'm trying to automate the webpage "http://www.quikr.com",when I open this you will get a pop up window first saying "Please Choose Your Location" then after closing it , I can see the main page of quikr.
I tried closing that Popup page by automation ,but not able to do
Tried using xpath
driver.findElement(By.xpath("//*[#id='csclose']/strong")).click();
Tried using className
driver.findElement(By.className("cs-close cs-close-v2")).click();
Tried using id
driver.findElement(By.id("csclose")).click();
Please help me with this
to close multiple popups in webdriver and switch to parent window
String parent = driver.getWindowHandle();
Set<String> pops=driver.getWindowHandles();
{
Iterator<String> it =pops.iterator();
while (it.hasNext()) {
String popupHandle=it.next().toString();
if(!popupHandle.contains(parent))
{
driver.switchTo().window(popupHandle);
System.out.println("Popu Up Title: "+ driver.switchTo().window(popupHandle).getTitle());
driver.close();
The Following Code Works for Me to Handle Pop Up/ Alerts in Selenium Webdriver
Just Copy Paste this Code After the Event which is triggering the Pop up/Alert i.e after clicking on save.
if(driver.switchTo().alert() != null)
{
Alert alert = driver.switchTo().alert();
String alertText = alert.getText();
alert.dismiss(); // alert.accept();
}
in your case you try to run this code at starting of the code bcz it will directly close the pop up
Since this is a JavaScript modal, when the page finishes loading the JavaScript code could still be running. The solution is to wait until the button to close the modal be displayed, close it and then follow with your test. Like this:
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(30));
wait.Until(ExpectedConditions.ElementIsVisible(By.Id("csclose")));
driver.FindElement(By.Id("csclose")).Click();
Tested myself and works fine.
Hope it helps.
i have tried it in ruby and this one works
see if this can help you in any way :)
require 'selenium-webdriver'
require 'test/unit'
require 'rubygems'
class Tclogin < Test::Unit::TestCase #------------ define a class----------------
def setup
##driver = Selenium::WebDriver.for :firefox
##driver.navigate.to "http://www.quikr.com" #---- call url----
##wait = Selenium::WebDriver::Wait.new(:timeout => 60) # seconds #----define wait------
end
def test_login
##driver.find_element(:css, "strong").click
end
end
you can also use follwing xpath
##driver.find_element(:xpath, "//a[#id='csclose']/strong").click
public void closePopup() throws Exception {
WebDriver driver = new InternetExplorerDriver();
driver.get("http://www.quikr.com/");
WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("csclose"))).click();
System.out.println("Successfully closed the start Popup");
}
Try driver.findElement(By.Id("csclose")).click(); I hope that will help
Simple pressing Alt + F4 buttons worked for me, e.g.:
driver.findElement(By.cssSelector("html body div div img")).sendKeys(Keys.chord(Keys.ALT, Keys.F4));