Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 无法使用webdriver在弹出窗口中单击按钮_C#_Xpath_Selenium Webdriver - Fatal编程技术网

C# 无法使用webdriver在弹出窗口中单击按钮

C# 无法使用webdriver在弹出窗口中单击按钮,c#,xpath,selenium-webdriver,C#,Xpath,Selenium Webdriver,我正在尝试使用SeleniumWebDriver单击一个按钮。可以很好地使用以下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(); 知道我做错了什

我正在尝试使用SeleniumWebDriver单击一个按钮。可以很好地使用以下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>

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

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

您可以尝试css选择器的contains运算符*

 List<WebElement> mylist = driver.findElements(By.cssSelector("button[class*='ui-corner-all']"));
    mylist[0].click(); 
您可以访问Selenium with c中CSS选择器教程的链接


路径表达式看起来不错。确定它们是等价的吗?或者,显示更多的输入文档,让其他人帮助查找。这样行吗?如果类名带有空格。我不这么认为。如果我错了,你能纠正我吗。不过,cssSelector或Xpath查询可以工作-