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
Selenium WebDriver能够定位xpath并对其执行操作,但仍会抛出“NoTouchElementException”_Selenium_Xpath_Selenium Webdriver_Webdriver_Nosuchelementexception - Fatal编程技术网

Selenium WebDriver能够定位xpath并对其执行操作,但仍会抛出“NoTouchElementException”

Selenium WebDriver能够定位xpath并对其执行操作,但仍会抛出“NoTouchElementException”,selenium,xpath,selenium-webdriver,webdriver,nosuchelementexception,Selenium,Xpath,Selenium Webdriver,Webdriver,Nosuchelementexception,下面是使用xpath单击对象的简单代码。我成功地找到了xpath并执行了所需的操作,但仍然没有抛出任何uchelementException String localobject=OR.getProperty(object); //Obtaining xpath from object repository we = driver.findElement(By.xpath(""+localobject+"")); executor.executeScript("arguments[0].cli

下面是使用xpath单击对象的简单代码。我成功地找到了xpath并执行了所需的操作,但仍然没有抛出任何uchelementException

String localobject=OR.getProperty(object); //Obtaining xpath from object repository
we = driver.findElement(By.xpath(""+localobject+"")); 
executor.executeScript("arguments[0].click();", we);
这是痕迹

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #\/\/\*\[text\(\)\=\'Log\ Out\'\]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.9.1', revision: '63f7b50', time: '2018-02-07T22:42:22.379Z'
System info: host: 'xxxxxxxx', ip: 'xx.xx.xx.xx', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_161'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities {acceptInsecureCerts: false, browserName: internet explorer, browserVersion: 11, javascriptEnabled: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), se:ieOptions: {browserAttachTimeout: 0, elementScrollBehavior: 0, enablePersistentHover: true, ie.browserCommandLineSwitches: , ie.ensureCleanSession: false, ie.fileUploadDialogTimeout: 3000, ie.forceCreateProcessApi: false, ignoreProtectedModeSettings: false, ignoreZoomSetting: false, initialBrowserUrl: http://localhost:17221/, nativeEvents: true, requireWindowFocus: false}, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}}
Session ID: 1617a9c2-cd03-4c77-8732-c4951fd83729
*** Element info: {Using=id, value=//*[text()='Log Out']}
    at sun.reflect.GeneratedConstructorAccessor32.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:160)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:371)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:417)
    at org.openqa.selenium.By$ById.findElement(By.java:218)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
    at config.Keywords.click(Keywords.java:129)
    at sun.reflect.GeneratedMethodAccessor12.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at executionEngine.DriverScript.execute_Actions(DriverScript.java:123)
    at executionEngine.DriverScript.execute_TestCase(DriverScript.java:106)
    at executionEngine.DriverScript.main(DriverScript.java:57)

错误说明了一切:

org.openqa.selenium.NoSuchElementException: Unable to find element with css selector == #\/\/\*\[text\(\)\=\'Log\ Out\'\]
// and
Element info: {Using=id, value=//*[text()='Log Out']}
表达式/*[text\=\'Log\Out\']绝对不是有效的css选择器。当前CSS_选择器不支持文本属性

表达式//*[text='Log Out']不是有效的id,而是一个潜在的xpath

解决方案 从对象存储库获取xpath时,请确保将其作为字符串调用,如下所示:

we = driver.findElement(By.xpath(localobject));

在@DebanjanB的回答中添加:

您也可以尝试将其用作元素定位器-

//*[contains(.,'Log Out')]

试试这个,让我知道它是否适用于您。

我使用了您上面的解决方案,但最终还是出现了相同的情况。我正在以类似的方式对其他xpath执行相同的单击操作,在所有其他情况下都可以正常工作。只有使用这个特定的xpath异常才会引发。我想问题是其他的。还是谢谢你的尝试。嗨,SK,我以前已经试过了。但我还是再试了一次,我得到了同样的例外。如果你能为我们展示页面的HTML代码,至少部分页面可以注销,我可以帮助你更好。注销这是我能分享的最大限度。我不允许分享更多。