Selenium 对于超链接,没有这样的元素例外

Selenium 对于超链接,没有这样的元素例外,selenium,Selenium,我在Win7和Internet Explorer 11上使用selenium webdriver 2.41.0,在单击“我的请求”链接时遇到问题。HTML看起来像这样 <html class="ltr" dir="ltr"> <head> <body class="navigator "> <div class="nav-wrapper"> <div id="div.c1

我在Win7和Internet Explorer 11上使用selenium webdriver 2.41.0,在单击“我的请求”链接时遇到问题。HTML看起来像这样

<html class="ltr" dir="ltr">
<head>
        <body class="navigator ">
            <div class="nav-wrapper">
                <div id="div.c16bf8b2089e8100af3b7af3edb7aefd" class="app_menu_div" style="display: block;" sclabel="false" extinct="true" appid="c16bf8b2089e8100af3b7af3edb7aefd">
                <span id="c16bf8b2089e8100af3b7af3edb7aefd" class="submenu" style="display:block;">
                    <table cellspacing="0" cellpadding="1" border="0" width="100%">
                        <tbody>
                            <tr id="module.dd9d893a089e8100af3b7af3edb7ae21" name="nav.module" moduleparent="c16bf8b2089e8100af3b7af3edb7aefd" moduletype="DIRECT" modulename="Dashboard" moduleid="dd9d893a089e8100af3b7af3edb7ae21">
                            <tr id="module.70fbfcf2089e8100af3b7af3edb7ae43" name="nav.module" moduleparent="c16bf8b2089e8100af3b7af3edb7aefd" moduletype="LIST" modulename="Workspace" moduleid="70fbfcf2089e8100af3b7af3edb7ae43">
                            <tr id="module.6b7b70f2089e8100af3b7af3edb7ae2c" name="nav.module" moduleparent="c16bf8b2089e8100af3b7af3edb7aefd" moduletype="DIRECT" modulename="Request" moduleid="6b7b70f2089e8100af3b7af3edb7ae2c">
                            <tr id="module.8eb40db6089e8100af3b7af3edb7ae75" name="nav.module" moduleparent="c16bf8b2089e8100af3b7af3edb7aefd" moduletype="LIST" modulename="My Requests" moduleid="8eb40db6089e8100af3b7af3edb7ae75">
                                <td class="noWrap" colspan="2">
                                    <img align="left" width="16" height="16" src="MyRequests_16.pngx"/>
                                        <h3 class="nav_menu_header">
                                            <a id="8eb40db6089e8100af3b7af3edb7ae75" class="menu" href="https://clouddev.service-now.com/sc_request_list.do?sysparm_userpref_module=8eb40db6089e8100af3b7af3edb7ae75&sysparm_query=active=true^opened_by=javascript:gs.getUserID()^EQ" data-cancelable="true" target="gsft_main" style="">My Requests</a>
                                        </h3>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </span>
            </div>
        </body>
    </html>
对于以上所有选项,我得到

NoSuchElementException:找不到具有xpath的元素

还有其他的选择吗


谢谢

它似乎在任何IFrame中,这就是为什么您没有得到xpath。首先切换到IFrame,然后使用此xpath单击web元素。

尝试以下操作:

WebElement myReq = driver.findElement(By.xpath("//a[text()= 'MyRequests']"));
myReq.click();

使用//a[contains@id,'8eb40db6089e8100af3b7af3edb7ae75']

//a[containstext,'My Requests']

任何一个都可以


继续从html代码中编码,我怀疑,这是菜单栏下的链接。 在这种情况下,您必须将鼠标悬停在菜单栏上,以便显示菜单项。然后,您可以使用以下代码单击菜单下的所需项目:

Actions act = new Actions(driver); act.movetoElement(driver.findElement(By.xpath("//div[@id='div.c16bf8b2089e8100af3b7af3edb7aefd']"))).build().perform(); WebElement myReq = driver.findElement(By.xpath("//tr[@id='module.8eb40db6089e8100af3b7af3edb7ae75']//a")); myReq.click(); 如果它不是菜单栏下的项目,并且直接显示在网页的前面,则只能使用以下代码:

WebElement myReq = driver.findElement(By.xpath("//tr[@id='module.8eb40db6089e8100af3b7af3edb7ae75']//a")); myReq.click();
谢谢你的回复。该链接不在菜单项下,并且始终显示,因此我尝试了您的代码,它返回了以下异常org.openqa.selenium.NoSuchElementException:找不到xpath=//tr[@id='module.8eb40db6089e8100af3b7af3edb7ae75']的元素//警告:服务器未提供任何堆栈跟踪信息OK。我碰巧看到您对@Bhupesh的回答的回复,其中您为webelement添加了一个图像。他是对的。它是一个框架元素。因此,必须切换到框架,然后对元素执行必要的操作。之后,您必须使用以下代码切换回默认内容:driver.switchTo.defaultContent;这将使您返回默认页面继续。干杯谢谢斯塔姆哈尼。我尝试了你的建议,它返回org.openqa.selenium.NoSuchElementException:找不到xpath=//a[text='MyRequests']的元素警告:服务器没有提供任何堆栈跟踪信息谢谢Bhupesh,我不认为有一个iFrame在HTML中似乎找不到它,但尝试按照您的建议使用driver.switchTo.framegsft_nav进行切换;并得到以下org.openqa.selenium.NoSuchFrameException:No frame found警告:服务器没有提供任何stacktrace信息我还发现以下代码在主/登录页driver.switchTo.framegsft_nav中工作正常;WebElement myReq=driver.findElementBy.linkTextMy请求;myReq.click;但是,当我尝试执行来自另一个页面的相同代码时,请求被提交并收到响应,它抛出了“未找到帧”异常。抛出未发现框架异常的页面在Firebug中看起来像这样。有什么想法吗?当您完成登录页面时,请在code driver.switchTo.defaultContent中使用这一行;它将焦点从当前帧移到webdriver。然后,当您再次移动到另一页时,切换到该页上的框架。我希望它能解决这个问题。谢谢你,阿尼尔,恐怕你的建议和其他人的结果一样。获取了以下org.openqa.selenium.NoSuchElementException:找不到xpath==a的元素[contains@id,'8eb40db6089e8100af3b7af3edb7ae75']警告:服务器未提供任何堆栈跟踪信息 WebElement myReq = driver.findElement(By.xpath("//tr[@id='module.8eb40db6089e8100af3b7af3edb7ae75']//a")); myReq.click();