如何使用 Webdriver 和 C# 通过 Selenium 定位并单击嵌套在多个框架和框架集中的元素 [英] How to locate and click on an element which is nested within multiple frame and frameset through Selenium using Webdriver and C#
问题描述
我有如下所示的 html 页面,我需要在类 clslogin 中单击登录.
我如何遍历以找到登录名.我将 C# 与 selenium Webdriver 结合使用.
使用 XPath (/html/body/div/table/tbody/tr[1]/td[3]/a) 我无法控制登录类,总是抛出找不到元素的错误.任何人都可以帮助我获得确切的 xpath.
<头></头><frameset name="mytestTopFrame" rows="*"...... ><frame name="mytestTopsubframe" src="index.html" width="100%"......... ><头></头><frameset name="mytest" rows="70,20..."...... ><frame name="mytestsubframe" src="menu.html" width="100%"......... ><body class="clsmenu" .......><div align="left"><table id="标题"......><tr class="toptitle" ...><td class="clicklogin" ....><a class="clslogin" href="linkref">登录</a></td></tr></tbody></frame></框架集></frame></框架集></html>
解决方案 根据您共享的 HTML 以点击带有文本的元素 登录 您必须诱导 WebDriverwait 两次以切换 2 个子 frame
,然后再次定位所需的元素,如下所示:
//切换到mytestTopsubframenew WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Name("mytestTopsubframe")));//切换到mytestsubframenew WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Name("mytestsubframe")));//定位需要的元素new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='clslogin' and @href='linkref']"))).Click();
注意:您不必考虑
的存在,并且可以安全地忽略这些标签.
I have html page like below and I need to click on Login inside the class clslogin.
How do I traverse to find the Login.
I'm using C# with selenium Webdriver.
With XPath (/html/body/div/table/tbody/tr[1]/td[3]/a) I'm not getting control on Login class, always element not found error is throwing. Can anybody help me to get exact xpath.
<html>
<head></head>
<frameset name="mytestTopFrame" rows="*"...... >
<frame name="mytestTopsubframe" src="index.html" width="100%"......... >
<html>
<head></head>
<frameset name="mytest" rows="70,20..."...... >
<frame name="mytestsubframe" src="menu.html" width="100%"......... >
<html>
<body class="clsmenu" .......>
<div align="left">
<table id="Title" ......>
<tbody>
<tr class="toptitle" ...>
<td class="clicklogin" ....>
<a class="clslogin" href="linkref">Login </a>
</td>
</tr>
</tbody>
</table>
</div>
</body>
</html>
</frame>
</frameset>
</html>
</frame>
</frameset>
</html>
解决方案 As per the HTML you have shared to click on the element with text as Login you have to induce WebDriverwait twice to switch through 2 child frame
and then again to locate the desired element as follows:
//SwitchTo mytestTopsubframe
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Name("mytestTopsubframe")));
//SwitchTo mytestsubframe
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt(By.Name("mytestsubframe")));
//Locate the desired element
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='clslogin' and @href='linkref']"))).Click();
Note: You don't have to consider the presence of <frameset>
and can safely ignore those tags.
这篇关于如何使用 Webdriver 和 C# 通过 Selenium 定位并单击嵌套在多个框架和框架集中的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文
登录
关闭
扫码关注1秒登录
发送“验证码”获取
|
15天全站免登陆