【问题标题】:how to find the xpath for given html code如何找到给定html代码的xpath
【发布时间】:2013-09-30 23:42:28
【问题描述】:

这是 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)`

帮我解决这个问题...

【问题讨论】:

    标签: java selenium-webdriver


    【解决方案1】:

    试试这个:

    //*[@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());
    

    打印:注销

    【讨论】:

    • 我需要上述行的 xpath 而不是链接按钮。它的链接文本我需要获取链接文本的值。谢谢你的回复
    猜你喜欢
    • 1970-01-01
    • 2022-01-09
    • 2014-10-05
    • 1970-01-01
    • 2021-01-10
    • 2021-03-11
    • 2017-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多