Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/322.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.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
如何通过SeleniumWebDriver和Java找到具有以下属性的Cancel按钮_Java_Selenium_Selenium Webdriver_Xpath_Css Selectors - Fatal编程技术网

如何通过SeleniumWebDriver和Java找到具有以下属性的Cancel按钮

如何通过SeleniumWebDriver和Java找到具有以下属性的Cancel按钮,java,selenium,selenium-webdriver,xpath,css-selectors,Java,Selenium,Selenium Webdriver,Xpath,Css Selectors,按钮HTML是: <button class="ui-button ui-corner-all ui-widget" type="button">Cancel</button> 取消 我尝试使用类名定位器和链接文本,请帮助。您可以使用以下任一解决方案找到按钮: css选择器: driver.findElement(By.cssSelector("button.ui-button.ui-corner-all.ui-widget[type='button']"));

按钮HTML是:

<button class="ui-button ui-corner-all ui-widget" type="button">Cancel</button>
取消

我尝试使用类名定位器和链接文本,请帮助。

您可以使用以下任一解决方案找到按钮:

  • css选择器

    driver.findElement(By.cssSelector("button.ui-button.ui-corner-all.ui-widget[type='button']"));
    
  • xpath

    driver.findElement(By.xpath("//button[@class='ui-button ui-corner-all ui-widget'][contains(text(),'Cancel')]"));
    

您可以尝试使用以下xpath定位元素:

driver.findElement(By.xpath("//button[contains(@class,'ui-button ui-corner-all')]"));

您使用的是哪种Web驱动程序?请在此处共享确切的HTML,因为answerlink文本仅适用于锚定标记,我相信。下面是正确的答案,我只是想补充一下为什么您的链接文本尝试不起作用。@BillHileman谢谢您提供的信息:)@这个虚拟机是SeleniumWebDriver 2.53.1非常感谢你…它工作了..实际上我太笨了,根本没有使用xpath。。