0

I am trying to click a button using selenium webdriver. Working fine with the following XPath

driver.FindElement(By.XPath("html/body/div[36]/div[3]/div/button[1]")).click();

it clicks the button fine but if I try to find it using class then it wont click it

driver.FindElement(By.XPath("//div[@class='ui-dialog-buttonset']/button[1]")).click();

Any Idea what I am doing wrong. Actual source code is as follows:-

<div class="ui-dialog-buttonpane ui-widget-content ui-helper-clearfix">

    ::before
    <div class="ui-dialog-buttonset">
        <button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">

    <span class="ui-button-text"></span>

</button>
<button class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" type="button" role="button" aria-disabled="false">

                <span class="ui-button-text"></span>
            </button>
        </div>
        ::after
    </div>

</div>
Mathias Müller
  • 22,203
  • 13
  • 58
  • 75
sam
  • 4,594
  • 12
  • 61
  • 111
  • The path expressions look fine. Sure that they are equivalent? (Alternatively, show more of the input document to let others help find out.) – Mathias Müller Apr 22 '15 at 19:45

2 Answers2

0

I see two buttons with the same class name. You could try this:

List<WebElement> list = driver.findElements(By.cssSelector("button[class=\"ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only\"]"));
//click on the first button
list.get(0).click();  
LittlePanda
  • 2,496
  • 1
  • 21
  • 33
  • This will work? if class name with space. I don't think so. Could you correct me if I'm wrong – saravana Apr 23 '15 at 05:22
  • Ya you might be right. A cssSelector or Xpath query would work though - http://stackoverflow.com/questions/7475449/webdriver-classname-with-space-using-java – LittlePanda Apr 23 '15 at 05:42
0

you can try the contains operator "*" of css selector.

 List<WebElement> mylist = driver.findElements(By.cssSelector("button[class*='ui-corner-all']"));
    mylist[0].click(); 

you can visit this link for CSS Selector tutorial in Selenium with c#

http://binaryclips.com/2015/02/16/css-selectors-for-selenium-webdriver/

joinsaad
  • 853
  • 9
  • 13