无法使用webdriver单击弹出窗口中的按钮

时间:2015-04-22 19:36:36

标签: c# xpath selenium-webdriver

我正在尝试使用selenium webdriver单击一个按钮。使用以下XPath正常工作

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

它点击按钮很好但如果我尝试使用类找到它然后它不会点击它

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

任何想法我做错了什么。实际源代码如下: -

<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>

2 个答案:

答案 0 :(得分:0)

我看到两个具有相同类名的按钮。你可以试试这个:

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();  

答案 1 :(得分:0)

你可以尝试使用css选择器的包含运算符“*”。

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

您可以使用c#

访问Selenium中CSS Selector教程的此链接

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

相关问题