大家好,有人可以帮助我,为什么我无法选择“从帐户中删除”按钮。如果您查看屏幕截图,selenium会成功点击显示弹出窗口的星座。然后,我想要selenium点击从帐户中删除按钮,但它不起作用。任何帮助表示赞赏
这是我的代码:
public void clickSpecialPricingRemoveFromAccount(){
webDriver.findElement(By.xpath("//*[@id='singleProductTR']/td[6]/div/div[2]/div/div/div/div[2]/button[1])")).click();
}
我收到的错误消息是:
org.openqa.selenium.InvalidSelectorException:给定的选择器// [@ id ='singleProductTR'] / td [6] / div / div [2] / div / div / div / div [2] / button [1])无效或不会产生WebElement。发生以下错误: InvalidSelectorError:无法找到带有xpath表达式的元素// [@ id ='singleProductTR'] / td [6] / div / div [2] / div / div / div / div [2] / button [1])由于以下错误: SyntaxError:表达式不是合法表达式。 命令持续时间或超时:31毫秒 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/invalid_selector_exception.html 建立信息:版本:'2.53.0',修订版:'35ae25b1534ae328c771e0856c93e187490ca824',时间:'2016-03-15 10:43:46' 系统信息:主机:'wdhl843a4bd282a8.corp.intuit.net',ip:'172.17.205.71',os.name:'Mac OS X',os.arch:'x86_64',os.version:'10 .11.6' ,java.version:'1.8.0_91' 驱动程序信息:org.openqa.selenium.firefox.FirefoxDriver 功能[{applicationCacheEnabled = true,rotate = false,handlesAlerts = true,databaseEnabled = true,version = 46.0.1,platform = MAC,nativeEvents = false,acceptSslCerts = true,webStorageEnabled = true,locationContextEnabled = true,browserName = firefox,takesScreenshot = true,javascriptEnabled = true,cssSelectorsEnabled = true}] 会议ID:6ef87e2b-beae-424c-9fdf-a3728e4995c4 ***元素信息:{Using = xpath,value = // * [@ id ='singleProductTR'] / td [6] / div / div [2] / div / div / div / div [2] / button [ 1])}
答案 0 :(得分:2)
org.openqa.selenium.InvalidSelectorException:给定的选择器// [@ id ='singleProductTR'] / td [6] / div / div [2] / div / div / div / div [2] / button [ 1])无效或不会导致WebElement
您提供的xpath
在语法上看起来不正确,应该是: -
//*[@id='singleProductTR']/td[6]/div/div[2]/div/div/div/div[2]/button[1]
您已在最后添加)
xpath
,但不应该在那里。
但您可以使用更具体的xpath
,而使用innerText
更加稳定,如下所示: -
webDriver.findElement(By.xpath(".//button[text() = 'Remove from Account']").click();
答案 1 :(得分:0)
我想你只有一个带popup-header-cpq的元素。你可以简单地使用css选择器:
webDriver.findElement(By.cssSelector(" div.popup-header-cpq button:first-of-type"))。click()。
答案 2 :(得分:0)
使用css,一个有效的选择器将是:
button[onclick*='displayPopoverRemoveEntitlement']