3

I am trying to fill a text box but I am getting unable to locate element.

I have tried finding the element

  • by class
  • by name
  • Added 10 secs wait

But I am still getting the same error.

Here is HTML

    <div class="innerWrap">
    <textarea aria-autocomplete="list" aria-controls="typeahead_list_u_0_j"
    aria-expanded="false" aria-haspopup="true" aria-label=
    "Write a birthday wish on his Timeline..." class=
    "enter_submit uiTextareaNoResize uiTextareaAutogrow uiStreamInlineTextarea inlineReplyTextArea mentionsTextarea textInput"
    cols="48" id="u_0_k" name="message_text" placeholder=
    "Write a birthday wish on his Timeline..." role="textbox" title=
    "Write a birthday wish on his Timeline..."></textarea>
</div>

My Code:

textbox =  driver.find_element_by_name('message_text')
textbox.send_keys('Happy Birthday! ')

The web page I am working on is Facebook Birthdays page

2 Answers2

3

This question has been answered before but the doc links that the answer points to is outdated, so here is the new updated "switch to"/switch commands selenium docs link

You need to use this command to switch your driver so that it runs in the correct window/frame/ etc.

Check which windows are open by running: first run driver.getWindowHandles(); to return a set of all windows

Then use driver.switchTo().window(handle);} #if switching to another window

or driver.switchTo().frame("yourframename");} #if switching to a frame

or driver.switchTo().alert(); #if switching to a popup

Hope this helps

A similar question

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81
  • I did eventually succeeded in finding the element but through a different approach. I tried changing window and frame but that didn't helped as I was already in the same window and there is no iframe in the code. So I downloaded the page source through `driver.page_source` and the name of element I wanted to target in the downloaded source is actually "message" and not "message_text". However when I check the name of this element in browser through inspect element it still shows "message_text". I am not sure why there is difference of element name. – Abdul Moiz Farooq Jul 22 '17 at 13:51
  • 1
    @AbdulMoizFarooq that's an interesting discovery. You should post it as an answer to help other people. Thanks for the update. – Rachel Gallen Jul 22 '17 at 14:12
2

So I was finally able to find the element and fix the above error by looking at the page source.

print(driver.page_source)

Page source showed the name of element was message and not message_text. However inspect element in browser still shows message_text.

Rachel Gallen
  • 27,943
  • 21
  • 72
  • 81