[英]java selenium navigate through a webpage using xpath
我正在尝试使用硒进行登录,导航,填写表格并下载文件。 当我尝试导航时,我需要单击一个链接,听起来很基本。 由于它没有ID或名称,因此我使用FirePath来获取xPath( .//*[@id='id25']/li[4]/a
id='id25']/li[4]/ .//*[@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();
这些都不起作用,硒总是返回“无法找到元素:”
的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.switchTo().defaultContent();
driver.switchTo().frame(driver.findElement(By.name("upper")));
driver.findElement(By.xpath(".//*[@id='id25']/li[4]/a")).click();
使用以上方法,硒将选择所需的框架,然后单击所需的链接。
完成html(格式不正确)后,xpath可以正常工作并找到所需的元素。 使用http://www.xpathtester.com/xpath测试
编辑
这是一个有效的html。 不知道它是否正是您的页面。
<document>
<html>
<!--<![endif]-->
<head id="Head1"/>
<body class="header" onload="SetFrameHeight()">
<form id="formm" action="UpperNavigationRibbon.aspx?header=noname" method="post">
<div class="aspNetHidden"/>
<div class="aspNetHidden"/>
<span id="spanContent">
<div id="page_container" class="header">
<ul id="global_nav">
<div id="primary_links">
<a class="eLogo" title="Home" href="javascript:menu_Controll('/Portal/Home.aspx')"/>
<div style="width:810px">
<div id="primary_nav">
<ul id="id25" class="clearfix">
<li/>
<li/>
<li/>
<li>
<a class="LI_Primary" title="Network Support" href="javascript:menu_Controll('/ContactAndhelp/ContactsAdmin.aspx?CategoryID=10')">Network Support</a>
</li>
<li/>
</ul>
</div>
</div>
</div>
</ul>
</div>
</span>
</form>
</body>
</html>
</document>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.