简体   繁体   English

如何使用 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#

I have html page like below and I need to click on Login inside the class clslogin.我有如下所示的 html 页面,我需要在类 clslogin 中单击登录。

How do I traverse to find the Login.我如何遍历以找到登录名。 I'm using C# with selenium Webdriver.我将 C# 与 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.使用 XPath (/html/body/div/table/tbody/tr[1]/td[3]/a) 我无法控制 Login 类,总是抛出 element not found 错误。 Can anybody help me to get exact xpath.任何人都可以帮助我获得确切的 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:根据您共享的HTML以单击带有文本的元素作为登录,您必须诱导WebDriverwait两次以切换 2 个子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();

Note : You don't have to consider the presence of <frameset> and can safely ignore those tags.注意:您不必考虑<frameset>的存在,并且可以安全地忽略这些标签。

You need to first change to the correct iframe to access the path who are below the iframe.您需要先更改为正确的 iframe 才能访问 iframe 下方的路径。 For that you can use driver.SwichtTo().Frame("your frame ID") .为此,您可以使用driver.SwichtTo().Frame("your frame ID") If this solution does not work, in this thread may be a solution the thread uses the same lines of code but it search for the parent and child nodes如果此解决方案不起作用,则在此线程中可能是该线程使用相同代码行但它搜索父节点和子节点的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何通过Selenium和C#在HTML中定位元素 - How to locate the element within the HTML through Selenium and C# Selenium WebDriver:无法找到元素(C#) - Selenium WebDriver: unable to locate element (C#) 如何在 Selenium WebDriver 中使用 C# 使用 XPATH 定位动态 Salesforce Lightning INPUT 元素 - How to Locate a DYNAMIC Salesforce Lightning INPUT Element with XPATH using C# in Selenium WebDriver 如何使用 Selenium WebDriver 和 C# 在 div 中定位按钮 - How to locate a button inside a div with using Selenium WebDriver and C# 如何使用 Selenium 和 C# 在 ol 中单击列表元素 - How to click on an list element within ol using Selenium and C# 如何在C#中使用Selenium在表中查找文本并引用元素单击另一个元素? - How to locate a text in a table and referring the element click on another element using Selenium in C#? 如何通过C#单击Selenium Webdriver中的DOM元素 - How to click the DOM element in selenium webdriver thru C# 无法使用C#在Selenium Webdriver脚本中使用XPath定位元素 - Unable to locate an element with XPath in selenium webdriver script with C# 如何单击Selenium WebDriver中不可见的元素? - How to click an element which is invisible in Selenium WebDriver? 如何使用 Selenium 和 C# 定位 li 标签内的元素 - How to locate the element inside li tag using Selenium and C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM