image click -> submit button(though it is not a button)

19 views
Skip to first unread message

Shas

unread,
Aug 7, 2016, 11:39:27 PM8/7/16
to webdriver
hi Guys,
This is my 2nd attempt to post a qeustion about an image which does not have any useful info to find element in  my application that I need to click it.

The HTML code is:
<a href="javascript:document.form1.submit()">
<img border="0" src="images/button_submitForm.gif">
</a>

I tried to click the image using the following commands but still not working.
//dr.findElement(By.linkText("Submit")).click();
dr.findElement(By.xpath("//input[@src='images/button_submitForm.gif']")).click();

I tried all the possible By class methods , none of them are working. could anyone help me with thsi?

Regds,
Shas

darrell grainger

unread,
Aug 8, 2016, 8:53:36 AM8/8/16
to webdriver
Not sure why you are posting this message twice. If you are posting a message and expect it to appear within 1 second it won't. Google Groups is an international messaging system. If you cannot wait 5 minutes to see if your message was posted and you cannot wait 24 hours (possibly more) to see if there is a good response then you want to find a different forum to post your questions. This is true for everyone posting here.

That said, you need to learn how HTML works. Selenium is a tool. I can hand you all the tools necessary to repair a car. Doesn't mean you know how to repair a car. You need to understand how a car works before you can repair it. Same thing with testing a website. If you don't understand how HTML, Javascript, HTTP, etc. work then you aren't really testing a website. The more you learn about these things the better a tester you will become.

The IMG tag does NOT react to a click event. You can register a javascript callback function to listen for a click event on an IMG tag but that is some advanced stuff. In your case, the A tag (called an anchor tag) does react to a click event. From a user point of view they see the IMG and A tag as one thing on the website. You cannot click one without clicking the other. So when I click the button I am sending a click event to the A and IMG tags. The IMG tag ignores it and does nothing. From Selenium, you can select one or both the tags to receive a click event. You have much more control from Selenium than you do from the web browser interface.

Since we know that the IMG does nothing with the click event, there is no point even sending a click event to the IMG tag. You need to find and click the A tag.

The commented out line is trying to find an A tag (By.linkText) with the text "Submit" associated with it. This would work if you had <a href="javascript:document.form1.submit()">Submit</a>. It would not work if the A tag wrapped an image. It would not work if the text was " Submit", "Submit " or " Submit " (notice the spaces). Your HTML snippet does not have the text "Submit" so it will fail.

The second findElement you are trying is looking for an INPUT tag. You have an A and IMG tag. There is no INPUT tag. So it will fail to find an INPUT tag and throw an exception. If you changed it to:

dr.findElement(By.xpath("//img[@src='images/button_submitForm.gif']")).click();

it would successfully click the IMG tag. It would not submit the form because clicking the IMG tag isn't want you want to do. What you want to do is find the A tag and click it. Something like:

dr.findElement(By.xpath("//a")).click();

will find the first A tag on the page and click it. This is probably not what you want. You probably want to craft an XPath which will find the one, correct A tag that will submit the form. Usually if I have something like:

<form name='form1'>
....
<a href="javascript:document.form1.submit()">
<img border='0' src='images/button_submitForm.gif">
</a>
</form>

then I might be able to use an XPath like "//form[@name='form1']/a". You didn't include enough of the HTML to see if this would work but that is the basic idea,
Reply all
Reply to author
Forward
0 new messages