java在Xpath中使用“AND”和“normalizespace”时在不同浏览器中遇到不同的错误
我的网页上有以下简单的按钮-
<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
,在那里我得到了预期的输出
我不知道是什么导致了这个问题,我使用了AND
和normalize-space
是这样吗?或者我做错了什么
使用selenium 3.0.1
# 1 楼答案
在
XPath
中使用大写的OR
或AND
是错误的(至少在Selenium
中是这样)。您应该使用or
/and
。所以试试看