【发布时间】:2014-10-22 18:15:36
【问题描述】:
我正在尝试使用 selenium 进行登录、导航、填写表格和下载文件。
当我试图导航时,我需要点击一个链接,听起来很基本。
由于它没有 id 或 name,我使用 FirePath 来获取 xPath(.//*[@id='id25']/li[4]/a) 然后我做了以下操作:
driver.findElement(By.xpath(".//*[@id='id25']/li[4]/a"));
Selenium 返回我 无法定位元素: 我在这里和那里做了一些阅读并尝试了一些不同的东西:
driver.findElement(By.linkText("Network Support")).click();
driver.findElement(By.xpath("//a[@title='Network Support']")).click();
driver.findElement(By.cssSelector(".LI_Primary"));
driver.findElement(By.xpath("[@id='id25']/li[4]/a"));
driver.findElement(By.xpath("/html/body/form/span/div[1]/div/div[1]/div/ul/li[4]/a")).click();
这些都不起作用,selenium 总是返回 "Unable to locate element:"
html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head id="Head1"></head>
<frameset id="EbpFrame" frameborder="0" marginwidth="0" marginheight="0" rows="111px,*">
<frame style="border:0px !important;" noresize="noresize" frameborder="0" framespacing="0" scrolling="no" marginheight="0" marginwidth="0" name="upper" src="/general/UpperNavigationRibbon.aspx?header=noname">
#document
<!DOCTYPE html>
<!--
[if lte IE 7 ]> <html class="ie7"> <![endif]
-->
<!--
[if IE 8 ]> <html class="ie8"> <![endif]
-->
<!--
[if IE 9 ]> <html class="ie9"> <![endif]
-->
<!--
[if (gt IE 9)|!(IE)]><!
-->
<html>
<!--
<![endif]
-->
<head id="Head1"></head>
<body class="header" onload="SetFrameHeight()">
<form id="formm" action="UpperNavigationRibbon.aspx?header=noname" method="post">
<div class="aspNetHidden"></div>
<div class="aspNetHidden"></div>
<span id="spanContent">
<div id="page_container" class="header">
<ul id="global_nav"></ul>
<div id="primary_links">
<a class="eLogo" title="Home" href="javascript:menu_Controll('/Portal/Home.aspx')"></a>
<div style="width:810px">
<div id="primary_nav">
<ul id="id25" class="clearfix">
<li></li>
<li></li>
<li></li>
<li>
<a class="LI_Primary" title="Network Support" href="javascript:menu_Controll('/ContactAndhelp/ContactsAdmin.aspx?CategoryID=10')"></a>
</li>
<li></li>
</ul>
</div>
</div>
<div class="clientLogo"></div>
</div>
</div>
<div class="bottomgrad"></div>
</span>
<input id="hdnSerVar" type="hidden" value="fjunior@timbrasil.com.br" name="hdnSerVar"></input>
</form>
<script type="text/javascript"></script>
</body>
</html>
</frame>
<frame style="border:0px !important;" onload="AppBodyTrackHistory()" noresize="noresize" frameborder="0" framespacing="0" scrolling="auto" marginheight="0" marginwidth="0" name="portalmain" src="/Portal/Home.aspx"></frame>
</frameset>
</html>
【问题讨论】:
-
那么,路径表达式是否适用于您的代码?
driver.findElement(By.xpath("//*"));呢? -
@MathiasMüller 我尝试使用 xpath 进行登录,工作正常(顺便说一句,它在同一类上),但我使用的是 whit ID,因为它更易于阅读。
-
@louis,它变成了编辑 cmets 和答案。