[英]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.