Clicking an image button using selenium webdriver after login - javascript

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.

Related

Button was not actually clicked but shows element.click() performed in selenium script

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"

browser pop up is not closing in selenium

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.

Webdriver: Firefox profile for disable/enable javascript popup dynamically

I am writing a test case where I have to close a java script popup. My code works fine in windows but I am deploying the executable in a centOS based server where it is getting error like below:
Element is not clickable at point (719, 9.899993896484375). Other element would receive the click: <li style="display: block; bottom: 0px;"
Can someone tell me what might be the problem?
I also tried the following but it does not work.
FirefoxProfile p = new FirefoxProfile();
p.setPreference("browser.popups.showPopupBlocker", false);
p.setPreference("browser.history.allowPopState", false);
p.setPreference("privacy.popups.showBrowserMessage", false);
p.setPreference("privacy.popups.usecustom", false);
driver = new FirefoxDriver(p);
I could solve the problem.
Instead of clicking the close button of the JS popup, I removed the popup by executing javascript by injection.
JavascriptExecutor jsExecutor = (JavascriptExecutor)driver;
jsExecutor.executeScript("return document.getElementsByClassName('dateInfo')[0].remove()");
Use to click using JavascriptExecutor :-
WebElement element = "Your Element";
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element );
Hope it will help you :)

Using JavascriptExecutor to sendKeys plus click on web element

I'm trying to open a link in a new tab, then switch to that tab, in a Firefox browser, using selenium in Java. It's my understanding that in order to do this, I need to use a send keys combination.
In order to open the link in the same window, I've been using something like this:
WebElement we = driver.findElement(By.xpath("//*[#id='btn']"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", we);
The above was working fine for me.
Now I'm trying to also sendKeys, as in below, which is not working:
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("keyDown(Keys.CONTROL)
.keyDown(Keys.SHIFT)
.click(arguments[0])
.keyUp(Keys.CONTROL)
.keyUp(Keys.SHIFT);", we);
Any advice? I can't figure out the correct syntax to sendKeys to JavascriptExecutor. I've seen some similar solutions using Actions, but this hasn't worked for me either.
try below code to open any link on page to new tab & switch to that tab. Perform operations there & go back to first tab for further execution.
WebDriver driver = new FirefoxDriver();
driver.get("http://stackoverflow.com/");
WebElement e = driver.findElement(By.xpath(".//*[#id='nav-questions']"));
Actions action = new Actions(driver);
action.keyDown(Keys.CONTROL).build().perform(); //press control key
e.click();
Thread.sleep(10000); // wait till your page loads in new tab
action.keyUp(Keys.CONTROL).build().perform(); //release control key
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //move to new tab
driver.navigate().refresh(); // refresh page
driver.findElement(By.xpath(".//*[#id='hlogo']/a")).click(); //perform any action in new tab. I am just clicking logo
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL + "\t"); //switch to first tab
driver.navigate().refresh();
driver.findElement(By.xpath(".//*[#id='hlogo']/a")).click();// refresh first tab & continue with your further work.I am just clicking logo

Selenium webdriver doesn't click on save button in chrome and firefox

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

Categories