2

I want to send 'username' using Selenium python, for the following html:

<div class="entryforms-elements">    
    <input id="entryforms-control-element" class="entryforms-element-text" type="text" autocomplete="off"></input>    
</div>

But none of the following method worked:

browser.find_element_by_xpath('//input[@id="entryforms-control-element"]').send_keys('username')  

browser.find_element_by_id('entryforms-control-element').send_keys('username')  

browser.find_element_by_class_name('entryforms-element-text').send_keys('username')     

Please help me!

  • are you waiting for sufficient amount of time so that your element on the page loads completely ? – abhi Feb 20 '14 at 04:16
  • Once you create a driver instance use this to wait for sufficient amount of time self.driver.implicitly_wait(20). Try this probably it will solve the issue (use the id to find the element) – abhi Feb 20 '14 at 04:19
  • @user3235542 can you post the error? I don't understand if the element selected is wrong or if your browser doesn't find the element because you call it before that it's loaded – Carlo 1585 Nov 24 '16 at 16:56

3 Answers3

2

You might want to check the size of the found element to be sure you are actually locating something.

If you are using firefox this might be getting in the way: selenium webdriver is clearing out fields after sendKeys had previously populated them

Or some javascript might be munging the field after your keys are sent (maybe some js firing after page load).

You also might try calling click() first and then sending the keypresses -- perhaps that will help focus the element if it's the issue mentioned above or something similar.

Community
  • 1
  • 1
knifewine
  • 46
  • 2
0

Are you sure that the select by Xpatch is correct?

in my opinion should be something like that:

browser.find_element_by_xpath(".//*[@id='entryforms-control-element']")

in every case just to be sure is better you clean and then you introduce your input

browser.find_element_by_xpath(".//*[@id='entryforms-control-element']").clear()
browser.find_element_by_xpath(".//*[@id='entryforms-control-element']").send_keys('username')

in any case I advice you to use Firefox with the plugin firebug and firepath to know for sure the correct xpath of the elements

Carlo 1585
  • 1,455
  • 1
  • 22
  • 40
-1

Jump on your browser developer tools and check how long it takes the element to load in the network tab. Then set a wait in your code between the load and element find based on this information.