ai didi

java - 在 Xpath 中使用 "AND"和 "normalize-space"时在不同的浏览器中出现不同的错误

转载 作者:行者123 更新时间:2023-11-29 04:35:42 24 4
gpt4 key购买 nike

我的网页中有以下简单按钮 -

<button type="button" class=" m-form-btn" onclick="myFunction()">Save</button>

由于类名有一个空格所以我使用 normalize-space() -

driver.findElement(By.xpath("//button[@type='button'][normalize-space(@class='m-form-btn')]")).click();

以正确的方式工作正常,但我确实以这种方式使用 AND-

driver.findElement(By.xpath("//button[@type='button' AND normalize-space(@class='m-form-btn')]")).click();

//or

driver.findElement(By.xpath("//button[normalize-space(@class)='m-form-btn' AND @type='button']")).click();

Firefox 中出现错误 -

Exception in thread "main" org.openqa.selenium.remote.UnreachableBrowserException: Error communicating with the remote browser. It may have died. Build info: version: 'unknown', revision: '1969d75', time: '2016-10-18 09:43:45 -0700' System info: host: 'TSS167', ip: '192.168.1.167', os.name: 'Windows 8.1', os.arch: 'amd64', os.version: '6.3', java.version: '1.8.0_91' Driver info: driver.version: RemoteWebDriver Capabilities [{rotatable=false, raisesAccessibilityExceptions=false, marionette=true, firefoxOptions={args=[], prefs={}}, appBuildId=20161208153507, version=, platform=XP, proxy={}, command_id=1, specificationLevel=0, acceptSslCerts=false, processId=7824, browserVersion=50.1.0, platformVersion=6.3, XULappId={ec8030f7-c20a-464f-9b0e-13a3a9e97384}, browserName=firefox, takesScreenshot=true, takesElementScreenshot=true, platformName=windows_nt}] Session ID: 30ed9c92-2d49-4c7b-83bc-c1638e24b3e8 at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:622) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368) at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473) at org.openqa.selenium.By$ByXPath.findElement(By.java:361) at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360) at ClassEight.main(ClassEight.java:28) Caused by: java.lang.IllegalArgumentException: expected one element but was: at com.google.common.collect.Iterators.getOnlyElement(Iterators.java:322) at com.google.common.collect.Iterables.getOnlyElement(Iterables.java:284) at org.openqa.selenium.remote.ErrorCodes.toStatus(ErrorCodes.java:138) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:92) at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:42) at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:163) at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:82) at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601) ... 5 more

并且在 Chrome 中出现相同的错误 -

Exception in thread "main" org.openqa.selenium.InvalidSelectorException: invalid selector: Unable to locate an element with the xpath expression //button[normalize-space(@class)='m-form-btn' AND @type='button'] because of the following error: SyntaxError: Failed to execute 'evaluate' on 'Document': The string '//button[normalize-space(@class)='m-form-btn' AND @type='button']' is not a valid XPath expression.

我使用 firepath evaluate 两个 xpath 得到了预期的输出。

我不知道是什么导致了这个问题,我正在使用 ANDnormalize-space 是这样吗?或者我做错了什么?

使用 selenium 3.0.1

最佳答案

XPath 中使用大写的 ORAND 是错误的方式(至少在 Selenium 中)。您应该改用 or/and。所以试试

driver.findElement(By.xpath("//button[normalize-space(@class)='m-form-btn' and @type='button']")).click();

关于java - 在 Xpath 中使用 "AND"和 "normalize-space"时在不同的浏览器中出现不同的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41691882/

24 4 0
文章推荐: php - Mysql只选择唯一行(只出现一次)
文章推荐: iphone - UIScrollView 使用系统中的两个 uiimageview 初始化?
文章推荐: MySQL 连接并从多个表中选择
文章推荐: java - spring 4 @RequestBody 中的未知参数验证
行者123
个人简介

我是一名优秀的程序员,十分优秀!

滴滴打车优惠券免费领取
滴滴打车优惠券
全站热门文章
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com