2

I want to locate element TableHeader which is inside a form table, it has the following page structure:

<form> 
  <table id="topNavigation" cellspacing="0" cellpadding="0" border="0" width="100%"> 
    <div class="PageContent"> 
      <table cellspacing="0" cellpadding="0" border="0" width="100%"> 
        <tbody> 
          <tr> 
            <td class="TableLeftCor"/>
            <td class="TableHeader"> Main </td> 
            <td class="TableRightCor"/>
          </tr> 
        </tbody>
      </table> 
      <table cellspacing="0" cellpadding="0" border="0" width="100%">
      <div id="copyright"> 
      </div> 
</form>

Can u please help how to locate table-header using driver.switchTo().frame().

  • I don't think switchTo does what you think it does. I see no Frame in your DOM therefor there is no reason to switch into a frame. driver.findElement(By.className("TableHeader")) should return your element without any problem. – Daniel Jul 10 '17 at 12:15
  • Thanks, let me try this. Can you explain how to know that element is inside frame? And what is the difference between form and frame tag? – Rose Jul 10 '17 at 12:23
  • Usually, when we talk about frames, we talk about iFrames. You can recognise them easily by their <iframe src="URL"> tags. They allow to include another HTML document in your website but, sinc they aren't part of "your" DOM the WebDriver can't find Element inside the iFrame from the outside, so you need to switch. Forms on the other hand usually define an action to be executed on all input elements inside the form and have no impact on the availability of your element. Try to read up on HTML and css, it really helps in the long run. – Daniel Jul 10 '17 at 15:06
0

Simple said to describe deference between Frame and Forms, is Frames are a sub-Window from main browser, so to handle objects inside we have to switch driver to this window. But Form is an organizing control to group HTML controls.

see Ref : https://www.w3schools.com/tags/tag_frame.asp

Locate Object in a Frame: 1- Define an object for the frame control. 2- switch the driver to this frame. 3- start finding and dealing with the elements inside the frame. 4- close the frame after finishing. 5- switch the driver again to the default content after closing the frame.

General Example Selenium/C# :

public IWebDriver Driver;
public IWebElement GeneralFrame;
Driver.SwitchTo().Frame(GeneralFrame); 

/*
locate your test objects by using findelment functions and deal with them.
after that make sure to close the frame.
then switch to default content.
*/

Driver.SwitchTo().DefaultContent();
  • Thanks now I got the difference between form and frame. May I know what is closing the frame and how to do it? – Rose Jul 10 '17 at 12:57
  • If the frame appears as a pop up window, then you should close that popup window by using the button that has close action in the frame, after finishing using controls inside the frame , so you can access default browser window. But if frame is not a popup window, then just skip closing step. – A.A.A Jul 10 '17 at 13:02
  • Hi, as suggested I tried to locate element with driver.findElement(By.className("TableHeader")) but still giving exception unable to locate element. This is what I tried - @FindBy(className = "TableHeader") private WebElement pageHeader; I tried xpath also but no luck, pls suggest also let me know if you need more details. Thanks! – Rose Jul 11 '17 at 1:54
  • Below is the DOM and i want to locate 'TableHeader' – Rose Jul 11 '17 at 1:55
  • <document> <html> <head> <body> <form> <table id="topNavigation" cellspacing="0" cellpadding="0" border="0" width="100%"> <div class="PageContent"> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <tbody> <tr> <td class="TableLeftCor"/> <td class="TableHeader"> Main </td> <td class="TableRightCor"/> </tr> </tbody> </table> <table cellspacing="0" cellpadding="0" border="0" width="100%"> <div id="copyright"> </div> </form> </body> </html> </document> – Rose Jul 11 '17 at 1:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.