My project is in mvc and I want to test it using selenium web driver.
Some button clicks work properly. But when I navigate to different page saying continue on that page Save button doesn't work.
Below is my code
driver.FindElement(By.Id("BtnAddNew")).Click();
IWebElement cat = driver.FindElement(By.Id("Cat"));
cat.SendKeys("Single-family house");
IWebElement ext_id = driver.FindElement(By.Id("ExternalId"));
ext_id.SendKeys("SAR_47");
IWebElement zip = driver.FindElement(By.Id("AddressZipTown"));
zip.SendKeys("1205 Genève");
IWebElement street = driver.FindElement(By.Id("AddressStreet"));
street.SendKeys("Tramstrasse 10");
driver.FindElement(By.Id("btnContinue")).Click();
driver.FindElement(By.Id("btnSave")).Click();
driver.Quit();
Can anyone solve my problem?
You can solve this problem by checking the DOM for a certain amount of time to try to find your WebElement. You can use Implicit or Explicit Waits.
Doc:
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-waits
http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#implicit-waits
Related
I'm writing an automation script for a Pega Web application. I have button click functionality, but actually the button is not getting clicked when i ran the script.When I check the logs, it shows that action was performed but it's not actually clicking the button.
I tried below actions but nothing works,
WebDriverWait wait = new WebDriverWait(driver, 80);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Submit')]")));
driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).click();
Then i tried below code,
WebDriverWait wait = new WebDriverWait(driver, 80);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[contains(text(),'Submit')]")));
WebElement button=driver.findElement(By.xpath("//button[contains(text(),'Submit')]"));
button.sendKeys(Keys.RETURN);
Also i tried javascript as well,
JavascriptExecutor jse = (JavascriptExecutor) driver;
jse.executeScript("arguments[0].click();", button);
I have faced similar issue multiple times, I fix it using -
while(driver.findElements(By.xpath("//button[contains(text(),'Submit')]")).size()!=0)
{
driver.findElement(By.xpath("//button[contains(text(),'Submit')]")).click();
}
This is just a temporary fix, I found it somewhere on Internet only. I believe it's some issue with the driver itself.
---- Edit ----
Before clicking on the button, can you make sure if the page is completely loaded(Ajax) by using below javascript commands-
jQuery.active ==0 && document.readyState == "complete"
After launching the url there is a email pop up that i am trying to close. i have written xpath and able to find the close icon with that. but when i am trying to execute it is not closing. then i added implicitly wait for the element to be visible and then trying to close. still it is not closing.
Can you please tell me what would be the reasons for such cases and how it can be solved.
and Submit button is also not clicking i have given the correct xpath.
Thanks in advance..
below is the code snippet.
public static void main(String[] args) {
WebDriver driver;
System.setProperty("webdriver.chrome.driver", "E:\\Softwares\\Chromedriver.exe");
driver = new ChromeDriver();
driver.get("https://html.com/input-type-file/");
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
WebElement closeButon = driver.findElement(By.xpath("//a[#class='om-close miami-element-close miami-close']"));
if(closeButon.isDisplayed())
{
System.out.println("close Buton is there.. ");
closeButon.click();
System.out.println("close Buton closed ");
}
driver.findElement(By.xpath("//input[#name='fileupload']")).sendKeys("E:\\Users\\laxman_p\\Desktop\\PromoFeature.txt");
//Submit button
driver.findElement(By.xpath("//*[#id='post-206']/div/div[3]/form/input[2]")).click();
}
If you cant close it by xpath,
I think this issue can be related with focused window,
You need to use getWindowHandle and switchTo methods to focus this pop-up.
After that you will be able to make process on this pop-up.
driver.getWindowHandles()
returns set of windows.
driver.switchTo.window(windowId);
will switch you to window which you want to focus on.
I am doing:
driver = new webdriver.Builder()
.forBrowser('safari')
.build();
var referrer = 'http://localhost:3000/tours/hood-river';
// console.log(referrer);
driver.get(referrer);
driver.findElement(By.id('requestGroupRate')).click();
//requestGroupRate is a link, so clicking it should move it to a new page
driver.wait(function(){
return driver.findElement(By.id('myThing')).then(function(element){
console.log("hereere");
assert(element.value === referrer);
done();
});
},10000);
I find that the findElement(By.id('myThing')), fails, even though the page it should be on clearly has 'myThing'. But if I change the line to
driver.findElement(By.id('requestGroupRate'))..
Then the element is found! This leads me to believe, that the click() does not cause the driver to navigate to the link.
EDIT: The link I am trying to click on:
<a id="requestGroupRate"
href="/tours/request-group-rate">Request Group Rate.</a>
you can simply add driver.sleep(10000) after click() for debug purpose.
if the page changed, means the link and click() worked and the possible failed reason it's when script click on the link, the page is still loading, so browser failed to response to the click event.
then you can move the driver.sleep(10000) after browser.get() to see click() can work or not.
As suggested by acdcjunior findout if element is clickable use browser developer tools. Chrome it is a side panel, Firefox it is a marker at the end of the HTML tag.
I have had issues getting Selenium to click on a GIS map. In that case i use an alternative (will need to translate this Java into Javascript):
((JavascriptExecutor) driver).executeScript("document.getElementById('map_container').dispatchEvent(new Event('click'));");
Also suggest testing your xpath in the browser developer console:
document.evaluate(".//tagName[#id='Query']", document,null, XPathResult.ANY_TYPE, null).iterateNext();
Or testing your cssSelector in the browser developer console:
document.querySelector('#idtext')
I'm trying to automate a login page using selenium-webdriver and I'm using IE as the browser. The problem that I'm experiencing is that after the login I'm not able to select the image button. I have even introduced time delay after the login credentials are entered still the script runs and the button doesn't gets clicked. P.S I have commented all the methods I have tried too.
The code I have used is:
System.setProperty("webdriver.ie.driver",
"C:/Program Files/IEDriverServer/IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.manage().window().maximize();
driver.navigate().to("website name");
driver.findElement(By.id("userid")).sendKeys("username");
driver.findElement(By.id("password")).sendKeys("password");
//driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
driver.findElement(By.id("submitButton")).click();
Thread.sleep(5000);
//driver.findElement(By.id("imgBtnAdd")).click();
//driver.findElement(By.cssSelector("a[href='Images/Go.gif']")).click();
driver.findElement(By.xpath("//img[# src='Images/Go.gif']")).click();
//System.out.println("Manual Click of Ok button");
//By xpath = By.xpath("//button[#name='imgBtnAdd'][#type='image'][contains(image(),'Images/Go.gif')]");
//WebElement myDynamicElement = (new WebDriverWait(driver, 10))
//.until(ExpectedConditions.presenceOfElementLocated(xpath));
//myDynamicElement.click();
driver.findElement(By.cssSelector("input[id='rblRoleGroup_4']")).click();
driver.findElement(By.id("imgBtnRoleGroup")).click();
//WebElement element = driver.findElement(By.id("imgBtnAdd"));
//Thread.sleep(5000);
//JavascriptExecutor executor = (JavascriptExecutor)driver;
//executor.executeScript("arguments[0].click();", element);
//driver.close();
You can try sendkkeys :
driver.findElement(By.id("imgBtnAdd")).sendKeys(Keys.ENTER);
If image is in new window try this:
for(String winHandle : driver.getWindowHandles())
{
driver.switchTo().window(winHandle);
}
Try click using JavascriptExecuter might help you -
WebElement element = driver.findElement(By.id("imgBtnAdd"));
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("arguments[0].click();", element);
After viewing your code, I have noticed that your xpath was wrong.
Instead of using <img> tag, use <input> tag.
driver.findElement(By.xpath("//input[#src='Images/Go.gif']")).click();
Explanation of xpath:- Use src attribute of <input> tag.
WebDriver driver = new FirefoxDriver();
driver.get("https://www.flipkart.com");
driver.manage().window().maximize();
String parentWindowHandler = driver.getWindowHandle(); // Store your parent window
String subWindowHandler = null;
Set<String> handles = driver.getWindowHandles(); // get all window handles
Iterator<String> iterator = handles.iterator();
while (iterator.hasNext()){
subWindowHandler = iterator.next();
}
driver.switchTo().window(subWindowHandler);
I tried it by switching to main window also. Please add valuable input or code to close the pop up.
You can try using the java Robot API by importing java.awt.Robot libraries. An example is here:
One solution for File Upload using Java Robot API with Selenium WebDriver by Java
You can try to use it similarly to press the Esc key. Pressing Esc on flipkart website gets rid of the pop-up.
The pop-up which appears on Flipkart's website is a simple HTML modal. Window handle is used when a new pop-up window needs to be accessed.
To close the pop-up just click on the cross on the top right corner of the pop-up. Use waits to ensure that selenium finds the WebElement.
Try this:
driver.get("https://www.flipkart.com");
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement cross = wait.until(
ExpectedConditions.visibilityOfElementLocated(By.className("close-icon")));
cross.click()