我的网页中有以下简单按钮-
<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
-中的获取错误
线程"main"中出现异常org.openqa.selenium.remote.UnreachableBrowser异常:错误与远程浏览器通信。它可能已经死了。生成信息:版本:未知,修订:1969d75,时间:2016-10-18 09:43:45-0700'系统信息:主机:"TSS167",ip:"192.168.1.167",os.name:"Windows 8.1",os.arch:"amd64",os.version:"6.3",java.version:"1.8.0_91"驱动程序信息:驱动程序版本:RemoteWebDriver功能[{rotable=false,raisesAccessibilityExceptions=false,marionette=true,firefoxOptions={args=[],prefs={}},appBuildId=20161208153507,版本=,平台=XP,代理={},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}]会话ID:30ed9c92-2d49-4c7b-83bc-c1638e24b3e8org.openqa.selene.remote.RemoteWebDriver.exexecute(RemoteWebDriver.java:622)在org.openqa.selene.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:368)在org.openqa.selene.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:473)网址:org.openqa.selene.By$ByXPath.findElement(By.java:361)org.openqa.selene.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:360)在ClassEight.main(ClassEight.java:28)由以下原因引起:java.lang.IollegalArgumentException:应为一个元素,但实际为:在com.google.common.collect.Iterators.getOnlyElement(Iterators.java:322)在com.google.common.collect.Iterabales.getOnlyElement(Iterables.java:284)在org.openqa.selene.remote.ErrorCodes.toStatus(ErrorCodes.java:138)在org.openqa.selene.remote.http.W3CttpResponseCodec.decode(W3CHttpResponceCodec.java:92)在org.openqa.selene.remote.http.W3CttpResponseCodec.decode(W3CHttpResponceCodec.java:42)在org.openqa.selene.remote.HttpCommandExecution.execute(HttpCommandExecutior.java:163)在org.openqa.selene.remote.service.DriverCommandExecution.execute(DriverCommandExecutior.java:82)在org.openqa.selene.remote.RemoteWebDriver.exexecute(RemoteWebDriver.java:601)…再增加5个
在Chrome
-中出现以下错误
线程"main"中出现异常org.openqa.selenium.InvalidSelectorException:无效选择器:无法使用xpath表达式定位元素//button[规格化空间(@class)='-form-btn'AND@type='button']由于以下错误:SyntaxError:无法执行对"Document"执行"evaluate":字符串'//button[normalize space(@class)='-form-btn'AND@type='button']'为不是有效的XPath表达式。
我使用firepath
执行了evaluate
和xpath
,其中我正在获得预期的输出。
我不知道是什么原因造成的问题,我正在使用AND
和normalize-space
。是这样吗?或者我做错了什么?
使用selenium 3.0.1
在XPath
中使用大写的OR
或AND
是错误的方式(至少在Selenium
中是这样)。您应该使用or
/and
。所以试试
driver.findElement(By.xpath("//button[normalize-space(@class)='m-form-btn' and @type='button']")).click();