首页 文章

如何从javascript href中找到xpath

提问于
浏览
1

方案是我需要检查网页上存在的所有默认元素 . 我正在尝试检查网络表上的可用标头 . 我能找到除名称为IP地址/主机名的 Headers 以外的所有 Headers

<div id="ctl00_ContentPlaceHolder1_RadGrid1_GridHeader" class="rgHeaderDiv" style="overflow: hidden;">
<table id="ctl00_ContentPlaceHolder1_RadGrid1_ctl00_Header" class="rgMasterTable rgClipCells rgClipCells" style="border-color:#6788BE;border-width:1px;border-style:solid;width:100%;table-layout:fixed;overflow:hidden;empty-cells:show;">
<colgroup>
<thead>
<tr>
<th class="rgHeader rgSorted" style="font-weight:bold;font-style:normal;text-decoration:none;text-align:left;" scope="col">
<a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl01$ctl05','')" title="Click here to sort" onclick="Telerik.Web.UI.Grid.Sort($find('ctl00_ContentPlaceHolder1_RadGrid1_ctl00'), 'HostIP'); return false;">IP Address / Host Name</a>
<input class="rgSortAsc" type="button" title="Sorted asc" onclick="javascript:__doPostBack('ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl01$ctl06','')" value=" " name="ctl00$ContentPlaceHolder1$RadGrid1$ctl00$ctl02$ctl01$ctl06">
</th>
</tr>
</thead>
<tbody style="display:none;">
</table>
</div>

我尝试使用下面的代码,但没有这样做 . driver.findElement(By.xpath(“// div [@ id ='ctl00_ContentPlaceHolder1_RadGrid1_GridHeader'] // a [contains(@ href,'IP Address / Host Name')]”));

driver.findElement(By.xpath(“// a [contains(@ href,'IP Address')]”));

driver.findElement(By.linkText(IP地址/主机名));

我尝试更改div id及其值ti表id和相应的值,但脚本失败说无法找到带有xpath的元素...

我的脚本总是说无法找到带有id ...或xpath的元素...请帮忙 .

1 回答

  • 1

    如果您无法以编程方式访问该元素,我的建议是使用Selenium IDE选择元素,然后转到 File > Export Test Case As... > Java / JUnit 4 / WebDriver 导出测试用例 .

    我自己尝试了这个,结果代码使用:

    driver.findElement(By.linkText("IP Address / Host Name"))

    它似乎在Selenium IDE中运行良好 . 我还没有测试生成的Java代码 . - 从你的语法来看,你似乎在使用Java .

    你说你已经尝试过 By.linkText() ,所以也许's a bigger problem? (I notice you don'在你的 By.linkText() 例子中有引号,但我认为这是一个错字 . )

相关问题