Re: [selenium-users] Unable to click on a button using Selenium Web Driver

13,762 views
Skip to first unread message

Dinesh s

unread,
Sep 17, 2012, 6:20:11 AM9/17/12
to seleniu...@googlegroups.com
Hi try with this...

X-path=//button[contains(text(),'Add')]






Thanks
DineshS

sateesh babu

unread,
Sep 17, 2012, 6:22:37 AM9/17/12
to seleniu...@googlegroups.com
Hi Hema,

try this

driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
//(or)
Thread.sleep(2000L);
driver.findElement(By.id("ext-gen142")).click();
 

On Mon, Sep 17, 2012 at 3:45 PM, Hema <hems.p...@gmail.com> wrote:
Hi All,

I am testing a Web application developed using extjs with the help of Selenium WebDriver. I am new to Selenium. When I attempt to click on a button in the webpage, it throws an error.

Unable to locate element: {"method":"id","selector":"ext-gen142"}

Given below is the piece of HTML code :

<button type="button" id="ext-gen142" class=" x-btn-text add">Add</button>

Please let me know how to resolve this.

Thanks in Advance,
Hema


--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/9A9Zwnz4NiIJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
SATEESH BABU.K

Hema

unread,
Sep 17, 2012, 6:24:35 AM9/17/12
to seleniu...@googlegroups.com
Hi Dinesh,

Thanks for your response. I just tried with your code.  driver.findElement(By.xpath("//button[contains(text(),'Add')]")).click();

I still get the same error: Unable to locate element: {"method":"xpath","selector":"//button[contains(text(),'Add')]"}

Please let me know if I am missing something.

Thanks,
Hema

Krishnan Mahadevan

unread,
Sep 17, 2012, 6:35:04 AM9/17/12
to seleniu...@googlegroups.com
Hema,

You would need to furnish more html source here in that case. 

perhaps your button is appearing in a different frame and you haven't switched to it.

Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"



--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/hbox0as8_J8J.

Hema

unread,
Sep 17, 2012, 7:04:05 AM9/17/12
to seleniu...@googlegroups.com
Hi Krishnan,

Attached is the complete HTML code of the page. I will have to click on the button 'Add' in the page. Tried the same with Selenium RC and it works. But I am in the process of migrating my scripts to Web Driver. So kindly help me out.

Thanks in Advance for your help.

Thanks,
Nivedhitha
HTML_Code.html

Darrell Grainger

unread,
Sep 17, 2012, 4:20:27 PM9/17/12
to seleniu...@googlegroups.com
Hema,

Selecting things by id is usually your best option. There can be multiple items which have the word 'Add' in them. In the page you provided there are two elements with the word 'Add' in them. This means:

    "//button[contains(text(),'Add')]"

will find two elements. You need to pass findElements a locator which will only return ONE element. Since the id attribute SHOULD be guaranteed to be unique for the page, you know that finding the id is the best option. The best option for this would be a CSS selector:

    "button#ext-gen142"

If you are using Java the full statement will be:

    WebElement button = driver.findElement(By.cssSelector("button#ext-gen142"));
    button.click();

There are a few reason this will not work. The most obvious one would be if the button was invisible to the user. For example, I have been a web site which has an agreement in a text box and below is a checkbox and ONE button. The checkbox is not set the ONE button is CANCEL. If I check the DOM I see there is an ACCEPT button but is it hidden. If I select the checkbox, it runs some javascript and the ACCEPT button becomes visible. So a real user would never just click the ACCEPT button. Instead they have to select the checkbox (accepting the agreement) then they can click the ACCEPT button. Maybe you need to do something before you click the Add button.

Other option is if the ID field has spaces about it. With XPath "ext-gen142" abd " ext-gen142" are two different strings. From Firebug or other developer tools you will always see "ext-gen142" but this isn't want the XPath engine sees. If you switch to CSS it is less concerned about spaces. If the above CSS does not work you can try:

    "button[id*=ext-gen142]"

Hope one of those two work for you.

Darrell

QuPi

unread,
Sep 18, 2012, 2:01:37 AM9/18/12
to seleniu...@googlegroups.com
You should try this with xpath

driver.findElement(By.xpath("//button[@id='ext-gen142']")).click();

NOTE:
Using ' not " in xpath.

Hema

unread,
Sep 18, 2012, 5:29:09 AM9/18/12
to seleniu...@googlegroups.com
Hi Darrell,

Thanks for your response. I tried it with both the options but unfortunately both are not working. As you have guessed, the page has 3 tabs and there is a button named 'Add' in all 3 tabs with three distinct 'ext-gen' ids. So I couldn't use "//button[contains(text(),'Add')]" . Also I got this id from firebug. Attached is the HTML content of the current page which I am testing. I arrived at this HTML content by plainly copying it from firebug. Please let me know if there are any other possibilities to overcome this issue.

Thanks,
Hema
HTML_Code.html

Hema

unread,
Sep 18, 2012, 5:41:47 AM9/18/12
to seleniu...@googlegroups.com
Hi,

Thanks for you reply. I tried the same for other web page and it worked. Its not working for the current page which I am testing. From the HTML content of the page, I could figure out that the button is placed in a toolbar. Is there something that I have to do by giving the entire path? Copying the xpath of the button returns "//*[@id='ext-gen142']" in firebug. Any inputs on this would be much helpful.

Thanks,
Hema

QuPi

unread,
Sep 19, 2012, 12:02:21 AM9/19/12
to seleniu...@googlegroups.com
With this format,

I don't think you can recognize the object by Selenium IDE.

Mike Riley

unread,
Sep 20, 2012, 11:48:47 AM9/20/12
to seleniu...@googlegroups.com
I am going to go out on a limb here and guess that these IDs are generated by some software when this page is produced.  So any change to the page and your IDs are no longer valid, so you can't do this with IDs.

You have made these buttons virtually in all respects, even down to the elements preceding them, but you can probably come up with an XPath that uniquely identifies each one if you have to.

This one would select the first:
"//button[text()='Add']"

Where this one would select the second:
"//button[text()='Add'][2]"

However, if they both do the exact same thing, you could just click on the first one and not worry about the second one.  I had that where identical button existed at the top and bottom of a table, so it didn't matter which one I clicked on and finding the first one is faster and easier.

Mike
Post

Malga Murali Khrishna

unread,
Jun 18, 2014, 4:20:12 AM6/18/14
to seleniu...@googlegroups.com
HI All,
         I am also facing the same problem as Hema, here the ID given by the is a dynamic ID.
Here we the but is put in a table format, here I am giving you the html sample of the button which I am not able to click.

This i am Using in Mozilla.

<div id="ext-gen2433" class="x-panel-body x-panel-body-noheader" style="padding: 12px; width: 412px; height: 67px;">

    <div id="ext-gen2435" class="x-form-item x-hide-label" tabindex="-1"></div>
    <div id="ext-gen2437" class="x-form-item " tabindex="-1">
        <label id="ext-gen2438" class="x-form-item-label" style="width:100px;" for="ext-comp-2086"></label>
        <div id="x-form-el-ext-comp-2086" class="x-form-element" style="padding-left:105px">
            <div id="ext-comp-2086" class=" x-plain mcw-file-upload" style="width: 307px;">
                <div class="x-plain-tl"></div>
                <div id="ext-gen2439" class="x-plain-bwrap">
                    <div class="x-plain-ml">
                        <div class="x-plain-mr">
                            <div id="ext-gen2441" class="x-plain-mc">
                                <form id="ext-gen2408" class="x-plain-body x-form" enctype="multipart/form-data" method="POST" style="background: none repeat scroll 0% 0% transparent; width: 307px;">
                                    <div id="ext-gen2443" class="x-form-field-wrap x-form-file-wrap" style="width: 307px; height: 22px;">
                                        <input id="ext-comp-2087" class="x-form-text x-form-field x-form-file-text" type="text" autocomplete="off" size="20" readonly="" tabindex="-1" style="width: 221px;"></input>
                                        <table id="ext-comp-2093" class="x-btn x-form-file-btn x-btn-noicon" cellspacing="0" style="width: 75px;">
                                            <tbody class="x-btn-small x-btn-icon-small-left">
                                                <tr></tr>
                                                <tr>
                                                    <td class="x-btn-ml"></td>
                                                    <td class="x-btn-mc">
                                                        <em class="" unselectable="on">
                                                            <button id="ext-gen2444" class=" x-btn-text" type="button"></button>
                                                        </em>
                                                    </td>
                                                    <td class="x-btn-mr"></td>
                                                </tr>
                                                <tr></tr>
                                            </tbody>
                                        </table>
                                        <input id="ext-comp-2087-file" class="x-form-usericon-upload" type="file" size="229" name="ext-comp-2087" tabindex="-1" style="width: 307px; height: 22px; top: 1px; left: 8px;"></input>
                                    </div>


This is what I am using to find the element: WebElement browsebnt= w.findElement(By.xpath("//*[@id='ext-comp-2081']//tbody/tr[2]/td[2]/em/button"));

Please suggest me the better option, 

Thanks,

Murali Krishna

Nasir

unread,
Jun 19, 2014, 1:28:18 AM6/19/14
to seleniu...@googlegroups.com

Try this.....

Thread.sleep(3000)
 
1) //button[@id='ext-gen142']

2) //button(text()='Add')

3) //button(Contains(text(),'Add'))

4) //button[@type='button', @id='ext-gen142']]

Ripon Al Wasim

unread,
Jun 19, 2014, 1:33:15 AM6/19/14
to seleniu...@googlegroups.com
hi,
It's better to use ID. Is button ID dynamic?


On Mon, Sep 17, 2012 at 3:15 AM, Hema <hems.p...@gmail.com> wrote:
Hi All,

I am testing a Web application developed using extjs with the help of Selenium WebDriver. I am new to Selenium. When I attempt to click on a button in the webpage, it throws an error.

Unable to locate element: {"method":"id","selector":"ext-gen142"}

Given below is the piece of HTML code :

<button type="button" id="ext-gen142" class=" x-btn-text add">Add</button>

Please let me know how to resolve this.

Thanks in Advance,
Hema

--
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.
To view this discussion on the web visit https://groups.google.com/d/msg/selenium-users/-/9A9Zwnz4NiIJ.

Malga Murali Khrishna

unread,
Jun 19, 2014, 4:49:47 AM6/19/14
to seleniu...@googlegroups.com
Hi

Yes button id is dynamic and when i check button using firepath, I am able to see button text
But when I run my script, it is unable to find the button text and it is empty i you can see in my previous post.

THRINATH

unread,
Jun 19, 2014, 5:22:28 AM6/19/14
to seleniu...@googlegroups.com
Try

driver.findElement(By.cssSelector("button.x-btn-text add")).click();


Thrinath



To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.

To post to this group, send email to seleniu...@googlegroups.com.

Malga Murali Khrishna

unread,
Jun 19, 2014, 6:23:32 AM6/19/14
to seleniu...@googlegroups.com
Hi

All buttons have the same class name,and no button text
I have tried it , but no use.

if you see carefully, in the button tag, there is no text visible. but you can see it on real button in application

Thanks

MMK

Malga Murali Khrishna

unread,
Jun 19, 2014, 7:51:28 AM6/19/14
to seleniu...@googlegroups.com
Hi,
I am able to locate the button using this below locator

WebElement browsebnt= w.findElement(By.xpath("//table[contains(@class, 'x-btn x-form-file-btn x-btn-noicon')]//tbody/tr[2]/td[2]/em"));
je.executeScript("arguments[0].click();", browsebnt);

But unable to click the button, even the click command is executed and I don't see any error message
I am attaching the screen print of the button
browse.png

Ripon Al Wasim

unread,
Jun 19, 2014, 8:06:58 AM6/19/14
to seleniu...@googlegroups.com
If it is dynamic you can try with the following xpath:

//button[starts-with(@id, 'ext-gen')]

Please let me know whether it is work or not.

Ripon Al Wasim


To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Malga Murali Khrishna

unread,
Jun 19, 2014, 9:30:24 AM6/19/14
to seleniu...@googlegroups.com
Hi Wasim

I have used locator suggested by you but still it does not work

Error:Exception in thread "main" org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with

But in this application all buttons starts with same ID

Please suggest me the solution

Thanks
MMK

THRINATH

unread,
Jun 19, 2014, 9:38:53 AM6/19/14
to seleniu...@googlegroups.com
Can you please share us, with your HTML snippet ? Thanks

Thrinath




For more options, visit https://groups.google.com/d/optout.



--
Regards,
Thirnath

Malga Murali Khrishna

unread,
Jun 19, 2014, 9:51:41 AM6/19/14
to seleniu...@googlegroups.com
Hi Thrinath,

Here is html code

                                                                left:105px">
                                                                    <div id="ext-comp-2074" class=" x-plain mcw-file-upload" style="width: 307px;">
                                                                        <div class="x-plain-tl"></div>
                                                                        <div id="ext-gen1797" class="x-plain-bwrap">
                                                                            <div class="x-plain-ml">
                                                                                <div class="x-plain-mr">
                                                                                    <div id="ext-gen1799" class="x-plain-mc">
                                                                                        <form id="ext-gen1766" class="x-plain-body x-form" enctype="multipart/form-data" method="POST" style="background: none repeat scroll 0% 0% transparent; width: 307px;">
                                                                                            <div id="ext-gen1801" class="x-form-field-wrap x-form-file-wrap" style="width: 307px; height: 22px;">
                                                                                                <input id="ext-comp-2075" class="x-form-text x-form-field x-form-file-text" type="text" autocomplete="off" size="20" readonly="" tabindex="-1" style="width: 221px;"></input>
                                                                                                <table id="ext-comp-2081" class="x-btn x-form-file-btn x-btn-noicon" cellspacing="0" style="width: 75px;">
                                                                                                    <tbody class="x-btn-small x-btn-icon-small-left">
                                                                                                        <tr></tr>
                                                                                                        <tr>
                                                                                                            <td class="x-btn-ml"></td>
                                                                                                            <td class="x-btn-mc">
                                                                                                                <em class="" unselectable="on">
                                                                                                                    <button id="ext-gen1802" class=" x-btn-text" type="button"></button>
                                                                                                                </em>
                                                                                                            </td>
                                                                                                            <td class="x-btn-mr"></td>
                                                                                                        </tr>
                                                                                                        <tr>
                                                                                                            <td class="x-btn-bl"></td>
                                                                                                            <td class="x-btn-bc"></td>
                                                                                                            <td class="x-btn-br"></td>
                                                                                                        </tr>
                                                                                                    </tbody>
                                                                                                </table>
                                                                                                <input id="ext-comp-2075-file" class="x-form-usericon-upload" type="file" size="229" name="ext-comp-2075" tabindex="-1" style="width: 307px; height: 22px; top: 6px; left: 30px;"></input>
                                                                                            </div>
                                                                                        </form>
                                                                                    </div>
                                                                                </div>
                                                                            
I am looking for the button in yellow

Malga Murali Khrishna

unread,
Jun 19, 2014, 11:36:14 AM6/19/14
to seleniu...@googlegroups.com
Hi 

Can we do anything with this information of that button


element {
}
.x-btn-noicon .x-btn-small .x-btn-text {
    height: 16px;
}
.mcw-dialog .innerpanel BUTTON, .mcw-dialog .innerpanel .mcw-graphic-button {
    font-family: Verdana;
    font-size: 10px;
    font-variant: normal;
    color: #000;
}
.ext-gecko .x-btn button, .ext-webkit .x-btn button {
    padding-left: 0px;
    padding-right: 0px;
}
.mcw-dialog BUTTON, .mcw-dialog .x-window-footer LABEL {
    font-family: Verdana !important;
    font-size: 11px !important;
    font-variant: normal;
    color: #000 !important;
}
.x-btn button {
    font: 11px arial,tahoma,verdana,helvetica;
    color: #333;
}
.x-btn button {
    border: 0px none;
    background-color: transparent;
    padding-left: 3px;
    padding-right: 3px;
    cursor: pointer;
    margin: 0px;
    overflow: visible;
    width: auto;
    outline: 0px none;
}
.x-btn-text {
    cursor: pointer;
    white-space: nowrap;
    padding: 0px;
}
.x-btn em {
    font-style: normal;
    font-weight: normal;
}
address, caption, cite, code, dfn, em, strong, th, var {
    font-style: normal;
    font-weight: normal;
}
.x-btn-mc {
    text-align: center;
    cursor: pointer;
    white-space: nowrap;
}
.x-btn {
    font: 11px tahoma,verdana,helvetica;
}
.x-btn {
    cursor: pointer;
    white-space: nowrap;
}
.x-form-field-wrap {
    text-align: left;
    white-space: nowrap;
}
.mcw-dialog .innerpanel .x-form-display-field, .mcw-dialog .innerpanel INPUT, .mcw-dialog .innerpanel .x-form-cb-label, .mcw-dialog .ext-mb-content SPAN, .mcw-dialog .innerpanel TEXTAREA, .mcw-dialog .x-form-item, .mcw-dialog .x-combo-list {
    font-family: Verdana !important;
    font-size: 11px;
    font-weight: normal;
    color: #333 !important;
}
.x-form-item {
    font: 12px tahoma,arial,helvetica,sans-serif;
}
.mcw-dialog .x-window-ml, .mcw-dialog .x-window-mr, .mcw-dialog .x-window-mc, .mcw-dialog .x-window-bl, .mcw-dialog .x-window-br, .mcw-dialog .x-window-bc {
    font-family: Verdana !important;
}
.x-window-mc {
    font: 11px tahoma,arial,helvetica,sans-serif;
}
element {
    visibility: visible;
}
.mcw-dialog {
    font-family: Verdana !important;
    font-variant: normal;
    text-transform: none;
}
body {
    font-family: "Myriad Pro","Myriad Web",Meiryo,tahoma,arial,verdana,sans-serif;
}


Thanks
MMK

Malga Murali Khrishna

unread,
Jun 20, 2014, 2:21:51 PM6/20/14
to seleniu...@googlegroups.com
Hi 

Thanks for all your support, I could solve my issue by the bellow type of locators
As the button which I was trying to click was having a combined control, I have taken the input tag of the combined control in the html code and access the button.This tag is type file

//input[@type='file']


Thanks
MMK

Ripon Al Wasim

unread,
Jun 23, 2014, 1:32:59 AM6/23/14
to seleniu...@googlegroups.com
1. Is that element hidden?
2. If it is not hidden, You can wait for that element to be visible by using "WebDriverWait". For using explicit wait (WebDriverWait) for a particular element you can visit:


Ripon Al Wasim


ankit kumar

unread,
Jul 9, 2014, 2:36:39 AM7/9/14
to seleniu...@googlegroups.com
Hello All,
I am facing the same issue which is discussed here.
i am using Selenium to test my Web Application whihc is based on EXTJS
I want to click on a "ADD" button which is having a "dynamic id" and there is no name property for the Add button please find the HTML code of the Add button below
 
 
<span id="bc_button-1135-btnInnerEl" class="x-btn-inner x-btn-inner-center" unselectable="on" style="line-height: 19px;">Add</span>
 
Thank You,

DongXiang

unread,
Jul 14, 2014, 3:04:00 AM7/14/14
to seleniu...@googlegroups.com
you can find this element by XPATH. I am not sure "Add" is unique or not on your web page. if it is, use it as the condition of your XPATH. Otherwise, you can check any unique information on this element as the XPATH condition. 



Date: Tue, 8 Jul 2014 23:36:39 -0700
From: akin...@gmail.com
To: seleniu...@googlegroups.com
Subject: [selenium-users] Re: Unable to click on a button using Selenium Web Driver
--
You received this message because you are subscribed to the Google Groups "Selenium Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to selenium-user...@googlegroups.com.
To post to this group, send email to seleniu...@googlegroups.com.

Ripon Al Wasim

unread,
Jul 14, 2014, 3:08:24 AM7/14/14
to seleniu...@googlegroups.com
You can use the following xpath:

//span[@class='x-btn-inner x-btn-inner-center']
//span[text()='Add']

Which portion of ID is changeable?


Vinodhini Purushothaman

unread,
Oct 10, 2014, 6:25:55 AM10/10/14
to seleniu...@googlegroups.com
You can use Javascriptexecutor instead of click function in selenium.

Sample code:
           JavascriptExecutor executor = (JavascriptExecutor)driver;
  executor.executeScript("arguments[0].click();", driver.findElement(By.name("Animation")));

<a href="http://www.indiumsoft.com/core-qa-offerings/test-automation" title="Test Automation"><strong>Test Automation</strong></a>

Reply all
Reply to author
Forward
0 new messages