如何使用 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 页面,我需要在 class clslogin 中单击“登录”。
如何遍历找到Login。
我正在使用 C# 和 selenium Webdriver。
使用 XPath (/html/body/div/table/tbody/tr[1]/td[3]/a) 我无法控制登录 class,总是抛出找不到元素错误。任何人都可以帮助我获得准确的 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>
您需要先改成正确的iframe才能访问iframe下面的路径。为此,您可以使用 driver.SwichtTo().Frame("your frame ID")
。如果此解决方案不起作用,则在此线程中 该线程使用相同的代码行,但它会搜索父节点和子节点
根据您共享的 HTML 单击带有文本 Login 的元素,您必须诱导 WebDriverwait两次切换2个child frame
然后再次定位所需元素如下:
//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();
注意:您不必考虑 <frameset>
的存在,可以安全地忽略这些标签。
我有如下 html 页面,我需要在 class clslogin 中单击“登录”。
如何遍历找到Login。 我正在使用 C# 和 selenium Webdriver。
使用 XPath (/html/body/div/table/tbody/tr[1]/td[3]/a) 我无法控制登录 class,总是抛出找不到元素错误。任何人都可以帮助我获得准确的 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>
您需要先改成正确的iframe才能访问iframe下面的路径。为此,您可以使用 driver.SwichtTo().Frame("your frame ID")
。如果此解决方案不起作用,则在此线程中
根据您共享的 HTML 单击带有文本 Login 的元素,您必须诱导 WebDriverwait两次切换2个child frame
然后再次定位所需元素如下:
//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();
注意:您不必考虑 <frameset>
的存在,可以安全地忽略这些标签。