How to click on image button in webdriver using java. The button type is image, plz find the relevant code in HTML code attached

2,504 views
Skip to first unread message

Umamaheshwar Thota

unread,
Apr 2, 2013, 8:10:39 AM4/2/13
to webd...@googlegroups.com

How to click on image button in webdriver using java. The button type is image, plz find the relevant code in HTML code attached. I have entered all the data and clicked on Apply now button, it is not at all saving. But when i try to create it manually, it is saved in less than 15 seconds.

Please find the attached screen shot.

The relevant code for the same:

//Navigating to Quick Application
driver.get(QAurl);
Thread.sleep(15000);
driver.findElement(By.id("DdlSalesPerson")).sendKeys("Swamy m Kumara");
driver.findElement(By.id("TxtFName")).sendKeys("Kumar");
driver.findElement(By.id("TxtLName")).sendKeys("Swamy");
driver.findElement(By.id("TxtAddress")).sendKeys("434, Main Road, Somajiguda");
driver.findElement(By.id("TxtZip")).sendKeys("79081");
driver.findElement(By.id("TxtSSN1")).sendKeys("881");
Thread.sleep(15000);
driver.findElement(By.id("TxtSSN2")).sendKeys("72");
driver.findElement(By.id("TxtSSN3")).sendKeys("4365");
Thread.sleep(5000);
driver.findElement(By.id("TxtDayPhone1")).sendKeys("963");
driver.findElement(By.id("TxtDayPhone2")).sendKeys("210");
driver.findElement(By.id("TxtDayPhone3")).sendKeys("5478");
Thread.sleep(5000);
driver.findElement(By.id("ChkIAgree")).click();
driver.findElement(By.id("TxtSignature")).sendKeys("Kumar Swamy");
Thread.sleep(5000);
System.out.println("Entered all the required fields");
//Reading the value in the image.
WebElement element = driver.findElement(By.id(OR.getProperty("FP_SImg_ID")));
String src = ((JavascriptExecutor)driver).executeScript("return arguments[0].attributes['src'].value;", element).toString();
img =src.split("=");
System.out.println("Value retrieved from the Image source: "+img[1]);
driver.findElement(By.id(OR.getProperty("FP_TxtSImg_ID"))).sendKeys(img[1]);
Thread.sleep(5000);
driver.findElement(By.id("TxtEmailId")).sendKeys("a...@abc.com");
driver.findElement(By.name("BtnSubmit")).click();
Thread.sleep(35000);
System.out.println("Successfully Applied from the QuickApp");

HTML code for the Apply now button:

<input id="BtnSubmit" type="image" style="height:33px;width:121px;border-width:0px;" 
onclick="javascript:return validateControls();" src="../Common/Images/HybridQA  
/apply_now.png" title="Submit Here" tabindex="45" name="BtnSubmit">

Please help me on this issue. Help will be appreciated.
QuickApp_Issue.jpg

Mike Riley

unread,
Apr 2, 2013, 2:05:52 PM4/2/13
to webd...@googlegroups.com
I am assuming there is a typo in your HTML and that it ends with this: />

I would think that using the name property would work, but you should also be able to use the ID property, which I think is supposed to be a bit faster and should be unique, while name doesn't have to be unique.

driver.findElement(By.id("BtnSubmit")).click();

Mike

Umamaheshwar Thota

unread,
Apr 3, 2013, 1:45:35 AM4/3/13
to webd...@googlegroups.com
Thanks Mike for your reply. I have tried all the possible ways using ID, Name and even xpath for the clicking on that element. But no success on that. Actually, as you can see, the "Apply Now" button is of type = image, for this reason i am not able to Save/Submit the Application.

Cheers,
Umamaheshwar Thota

Mike Riley

unread,
Apr 3, 2013, 12:13:42 PM4/3/13
to webd...@googlegroups.com
Yes, I saw that, but since it has an onclick attribute I would think that click() would work on it regardless of the type.

Can you try to create a simple page with just that button and verify that it also fails?

If it fails on a simple page like that I would think you have a bug and the test page would give a good reproducible test case.

Mike

Anurag Mala

unread,
Apr 6, 2013, 11:21:39 AM4/6/13
to webd...@googlegroups.com
It's something interesting.... I Would suggest you to try a Injecting JavaScript via selenium.. There are 2 approaches...

1. Try to Click the Apply Button via Javascript
WebDriver driver;
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('theform').submit();");
2. Directly call the JS function (Being called on click of Apply Button), instead of clicking the submit button (if valid)
js.executeScript("validateControls();");

I hope this helps :)
Reply all
Reply to author
Forward
0 new messages