Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
454 views
in Technique[技术] by (71.8m points)

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.

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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...