How to click a dynamic button

392 views
Skip to first unread message

jsdoomsday

unread,
Feb 8, 2010, 7:40:23 PM2/8/10
to Selenium Users
Hi everyone,

Thanks in advance for any help,

What im trying to do is click on a dynamic button the selenium code
looks like:
selenium.click("//button[@id='payout' and @name='payout' and
@type='button' and @onclick=\"genericSubmitForm('frmPayoutCK-M', this,
true, validateTransactionForm( 'CK-M' , 'PAYOUT',20, 200 ));\"]");

basically most of this information is Dynamic such as CK-M, and the
( 'CK-M' , 'PAYOUT',20, 200 )

What i would like to do is select this button without knowing the
dynamic information, i have attmepted to use wildcard string searched
but have had no luck, also i have tried to use the selenium.click("//
button[@id='payout') portion only as for some reason the execution of
this command passes but nothing is actually clicked and then the
script will eventually fail on a time out.

Thanks again,

Rohit Maurya

unread,
Feb 8, 2010, 10:26:57 PM2/8/10
to seleniu...@googlegroups.com
Hi,

try using selenium.click("//input[(@id='payout')]);

Rohit



--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To post to this group, send email to seleniu...@googlegroups.com.
To unsubscribe from this group, send email to selenium-user...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/selenium-users?hl=en.


SiKing

unread,
Feb 9, 2010, 12:41:06 PM2/9/10
to Selenium Users
Try just click("payout"). http://seleniumhq.org/docs/04_selenese_commands.html#locating-elements
see description for "identifier".
If that does not work, then there is obviously something else
(JavaScript events?) going on behind the scenes.

HTH.

darrell

unread,
Feb 9, 2010, 4:37:37 PM2/9/10
to Selenium Users
Without seeing the HTML that is generated it is hard to say why your
attempts have failed.

Assuming the tag is actually a button, the page does not violate the
unique id requirement of HTML and the id is not dynamic, you should be
able to locate it with the xpath "//button[@id='payout']".

Something things I have found:

- if I don't prefix the xpath locators with "xpath=" it occasionally
fails to work. So try "xpath=//button[@id='payout']"
- if there are spaces in the id string it will not find it. I use
contains() to get around this, i.e. "xpath=//
button[contains(@id,'payout')]"
- attributes are case sensitive when using xpath. If it is 'payOut',
'Payout' or 'PAYOUT' then 'payout' will not match.

Those are the typical gotchas for using xpath with Selenium.

Additionally, if you are using Java and something like Eclipse to
develop the code then you can do things like set a breakpoint and use
the Expression (Watch) Windows to interactively try different
locators. I like to create a method like:

public void testMethod() {
String locator = "xpath=//button[contains(@id,'payout')]";
selenium.click(locator);
}

I then set a breakpoint on the click line. When it hits there I'll
evaluate: selenium.isElementPresent(locator) in the watch window. If
it is false I know the locator is not working for me. I'll edit the
locator string and save. Eclipse will reset the stack to the start of
the method so I can step to the click line again. Look at the watch
window to see if the isElementPresent is true. If not, edit the
locator, save, step to click line. I can keep doing this until I find
a locator that works.

Darrell

jsdoomsday

unread,
Feb 9, 2010, 8:08:35 PM2/9/10
to Selenium Users
Thanks for all the help everyone,

I have attempted the above suggestions the xpath ones came the closest
as selenium thinks it has clicked the button but it actually has not
and eventually timesout with the waitforpage method. I took a closer
look at the page source and it appears that there are multiple payout
buttons however the onclick information is different which is in the
format of onclick="genericSubmitForm('frmPayoutMB', this, true,
validateTransactionForm( 'MB' , 'PAYOUT',20 ));"


This information is dynamic i will know the MB part of it by passing
in a variable of which page i am on ill try to play aroudn with xpath
and that portion of the button, any other suggestions will be great.

Thanks again

jsdoomsday

unread,
Feb 9, 2010, 8:08:44 PM2/9/10
to Selenium Users
Thanks for all the help everyone,

I have attempted the above suggestions the xpath ones came the closest
as selenium thinks it has clicked the button but it actually has not
and eventually timesout with the waitforpage method. I took a closer
look at the page source and it appears that there are multiple payout
buttons however the onclick information is different which is in the
format of onclick="genericSubmitForm('frmPayoutMB', this, true,
validateTransactionForm( 'MB' , 'PAYOUT',20 ));"


This information is dynamic i will know the MB part of it by passing
in a variable of which page i am on ill try to play aroudn with xpath
and that portion of the button, any other suggestions will be great.

Thanks again

On Feb 9, 1:37 pm, darrell <darrell.grain...@gmail.com> wrote:

darrell

unread,
Feb 10, 2010, 2:16:44 PM2/10/10
to Selenium Users
So if I understand you correctly, there are multiple buttons with the
id='payout'? This is a violation of the HTML/XHTML standard. There are
some standards violations I can live with but this is just REALLY
sloppy.

If I was coding this, the id would be dynamic. I would use the
parameters of the onclick to form the id, e.g.
id='payout_MB_PAYOUT_20'. If this cannot be fixed and you know you are
looking for the input with specific onclick parameters try using:

xpath=//BUTTON[contains(@onclick,'MB') and
contains(@onclick,'PAYOUT')]

If there are multiple id with the same value, I would not use them.
Code should be safe to assume the id attributes on a page are unique.
If the page is violating this assumption then there is no guarantee
DOM or CSS parsers are going to work correctly. Basically, //
*[@id='payout'] should only EVER return one element from the current
DOM.

Darrell

jsdoomsday

unread,
Feb 10, 2010, 7:18:33 PM2/10/10
to Selenium Users
Thanks Darrell for all of your help, I completely agree having the
same ID name throughout the code makes things extremely difficult, im
not to sure why this was done but im going to try to find out :)

I was able to get it working using your suggestions selenium.click("//
button[contains(@onclick, 'frmPayout"+payout_code+"')]"); where
payment code is the MB part (also dynamic).

Thanks again.
Jas

jsdoomsday

unread,
Feb 10, 2010, 7:18:39 PM2/10/10
to Selenium Users
Thanks Darrell for all of your help, I completely agree having the
same ID name throughout the code makes things extremely difficult, im
not to sure why this was done but im going to try to find out :)

I was able to get it working using your suggestions selenium.click("//
button[contains(@onclick, 'frmPayout"+payout_code+"')]"); where
payment code is the MB part (also dynamic).

Thanks again.
Jas

On Feb 10, 11:16 am, darrell <darrell.grain...@gmail.com> wrote:

darrell

unread,
Feb 11, 2010, 5:16:26 PM2/11/10
to Selenium Users
Having the same id attribute throughout the code doesn't just make it
extremely difficult to automate the application but this is a total
violation of HTML standards. If you need a little industry support
here is a link to the official HTML 4.01 documentation:

http://www.w3.org/TR/html401/struct/global.html#adef-id

Here is the link for XHTML 1.0:

http://www.w3.org/TR/xhtml1/#h-4.10

And finally, for HTML 5:

http://dev.w3.org/html5/spec/Overview.html#the-id-attribute

Darrell

Reply all
Reply to author
Forward
0 new messages