23

This is the HTML file I have. I am trying to use Selenium-Webdriver API along with ChromeDriver to send_keys to an input filed inside the <body>. But I can't access anything which is inside of #document. I cannot figure out why. Can someone please tell me what this #document means and how can I access any of the elements inside this using Selenium.

<html>
<head>…<head>
<frameset >
    <frame>...</frame>
            <frame name="mainFrame" src>
                #document
                    <html>
                      <head>…</head>
                      <body>…</body>
                    </html>
        </frame>
    </frameset>
</frameset>

This is a router webpage, the actual webpage is HUGE, so I haven't pasted it here.

The router webpage

deepng
  • 809
  • 2
  • 9
  • 20
  • 1
    I would ideally want to know how to access elements inside this #document. I have tried the traditional ways using `find_elements_by_xxx` but haven't got any result. I would also like to know what #document means, because I couldn't get an answer by Googling it. – deepng Jan 31 '14 at 07:31
  • Ok, thanks @user2864740. I just added screenshot to my original post. If you see that, the actual HTML I need to use is sitting inside #document. @user1177636 I have tried, switching frames and to default_content, etc.. nothing has helped. I can't find these elements even from the Chrome dev tool console when I use commands like `document.getElementsbyName('')`. Seems like everything under #document is invisible. – deepng Jan 31 '14 at 07:39
  • 2
    @deepng That's a "virtual element". It's a Chrome-specific way of indicating that it is part of a *different* (embedded) DOM - you cannot use or query it (because the "virtual element" does not exist as part of the DOM) from code. – user2864740 Jan 31 '14 at 07:41
  • 1
    @deepng So don't look for "#document", but look for the task, such as "selenium frames" - http://stackoverflow.com/questions/15464808/how-to-navigate-a-subframe-inside-a-frameset-using-selenium-webdriver , http://stackoverflow.com/questions/6060557/selenium-problem-accessing-elements-within-frames?rq=1 YMMV – user2864740 Jan 31 '14 at 07:43
  • @user2864740, thanks, it worked. I have put the code below, just for future reference. Here since there are multiple frames we have to switch to the default content and then move to the frame of our choice. `driver.switch_to_default_content()` `frame =driver.find_element_by_name('mainFrame')` `driver.switch_to_frame(frame)` `elem = driver.find_element_by_xpath('//input[@name="ui_pws"]')` – deepng Jan 31 '14 at 09:00
  • @deepng Cool, glad you found a solution. Put your solution into an *answer* and then accept it later when you can. That will help most - and be easier to see! – user2864740 Jan 31 '14 at 09:05

2 Answers2

21

Just to summarize on what I learnt and implemented.

  1. #document is a virtual element, which doesn't really mean anything.

  2. If you have mulitple frames/framesets, you will have to switch frames.

    a. so first get to the default content. driver.switch_to_default_content()

    b. then get to the frame that you want to work with. frame = driver.find_element_by_name('mainFrame')

  3. Then play with the elements in that frame.

ggorlen
  • 44,755
  • 7
  • 76
  • 106
deepng
  • 809
  • 2
  • 9
  • 20
0

I have tried to do the same process in C#, and this worked for me. (I leave the code here for someone who maybe is looking at this in C#). I hope this will be useful for someone!

var FrameName = driver.FindElement(By.Name("...Frame Name..."));    
driver.SwitchTo().Frame(FrameName);    
var elem = driver.FindElement(By.XPath("... Xpath from in of the 
        Frame..."));    
elem.Click();
Dave
  • 3,073
  • 7
  • 20
  • 33