0

这是 HTML 代码

<td align="right" style="width: 50%">
<span style="padding-right: 10px">
<span id="ctl00_lblLoginUserName" style="font-style:italic;">You are logged in as OSAMIEE    </span>
</span>
<a id="ctl00_LinkButton1" href="javascript:__doPostBack('ctl00$LinkButton1','')">Log Out</a>

当我将此xpath用于此行时,出现以下错误:

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to find element with xpath == //*[@id='ctl00_lblLoginUserName'] (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 431 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '2.33.0', revision: '4e90c97', time: '2013-05-22 15:33:32'
System info: os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.7.0_40'
Session ID: 8f0644de-fe65-4d34-a960-ea80a6202f2f
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{platform=WINDOWS, elementScrollBehavior=0, javascriptEnabled=true, enablePersistentHover=true, ignoreZoomSetting=false, browserName=internet explorer, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss, version=8, cssSelectorsEnabled=true, ignoreProtectedModeSettings=true, requireWindowFocus=false, handlesAlerts=true, initialBrowserUrl=, nativeEvents=true, browserAttachTimeout=0, takesScreenshot=true}]
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:191)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:145)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:554)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:307)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:404)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:344)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:299)
    at login.Verification.main(Verification.java:173)`

帮我解决这个...

4

1 回答 1

0

尝试这个:

//*[@id='ctl00_LinkButton1']

此外,如果链接所在的页面在您的代码尝试单击它之前未完全加载,则该元素可能尚不可见(这也可能导致异常。在这种情况下,请使用 WebDriverWait 等到该元素加载。

WebDriverWait wait = new WebDriverWait(driver, TIMEOUT); // where TIMEOUT is for example 500 or 1000
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='ctl00_LinkButton1']")));
element.click();

编辑:

WebDriverWait wait = new WebDriverWait(driver, 1000);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='ctl00_lblLoginUserName']")));
System.out.println(element.getText());

打印:您以 OSAMIEE 身份登录

WebDriverWait wait = new WebDriverWait(driver, 1000);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='ctl00_LinkButton1']")));
System.out.println(element.getText());

打印:注销

于 2013-09-25T11:57:37.340 回答