1

The element where I want to replace text:

<input tabindex="1" style="padding-left:119px!important;width:318px!important;background-color:#fff;outline: none;box-sizing: inherit;" id="email" name="mobile" placeholder="Enter your Mobile Number" class="un_s un1_s" value="" onblur="remove_border();" type="text" maxlength="20">

I have tried various ways(all I could think up and google), to replace the placeholder text. Lots of errors later, I thought since the focus is already in the text box(where I want to enter the text may be just send_keys would work. It didn't. Can someone help me out and explain the concept or point to where I might read on where I went wrong?

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

chrome_path = r"C:\Users\-------\Desktop\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("SAMPLE WEBSITE URL")  #sorry had to remove the link :(
driver.maximize_window()

action = webdriver.ActionChains(driver)
elm = driver.find_element_by_id("user_sign_in").click()

inputElement = driver.find_element_by_xpath('//*[@id="lfm"]/div[1]/div[2]')     #driver.find_elements_by_xpath("//*[contains(text(), 'Enter your Mobile Number')]")     
#driver.find_element_by_id("mobile")
inputElement.send_keys('1234567890')
inputElement.submit()


#//*[@id="lfm"]/div[1]/div[2] xpath id for the mobile number element
#code below this is not working, for move mouse
#action = webdriver.ActionChains(driver)
#action.move_to_element((By.XPATH, '//*[@id="user_sign_in"]')).perform()



#For moving the mouse to sign in: Tried the ones below and they didn't work either
#driver.move_to_element(By.XPATH, '//*[@id="user_sign_in"]')
#login_menu = WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="user_sign_in"]')))
#ActionChains(driver).move_to_element(sign_in).perform()

Errors:

Traceback (most recent call last): File "C:/Python34/Selenium 2nd Trial.py", line 16, in inputElement.send_keys('9810307369') File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 322, in send_keys self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': keys_to_typing(value)}) File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webelement.py", line 457, in _execute return self._parent.execute(command, params) File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 233, in execute self.error_handler.check_response(response) File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 194, in check_response raise exception_class(message, screen, stacktrace) selenium.common.exceptions.WebDriverException: Message: unknown error: cannot focus element (Session info: chrome=55.0.2883.87) (Driver info: chromedriver=2.27.440174 (e97a722caafc2d3a8b807ee115bfb307f7d2cfd9),platform=Windows NT 10.0.14393 x86_64)

Bellerofont
  • 1,081
  • 18
  • 17
  • 16
Sid
  • 3,749
  • 7
  • 29
  • 62
  • How is your question different from [this one](http://stackoverflow.com/q/18557275/1426065)? – MattDMo Jan 16 '17 at 18:56
  • How does your current approach fail? Any errors? – alecxe Jan 16 '17 at 19:30
  • @alecxe, updated with the errors, I was trying something else and forgot to change the find_elements to find_element as suggested by you earlier. MattDMo I tried to follow along with that answer but the send_keys approach is not working due to the updated error in the edited post. – Sid Jan 16 '17 at 19:46

1 Answers1

0

You are trying to send keys to the div element, but meant to use an input element:

inputElement = driver.find_element_by_id('email')   
inputElement.send_keys('1234567890')
inputElement.submit()

And, be aware that the page might take some time to load and there could be visual effects preventing an element to be located or interactable. If this is the case, use WebDriverWait with an appropriate Expected Condition to make sure element is visible.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • This works perfectly. I have just started approaching the problem in a slightly different way after seeing this example. Since I am a beginner I am trying hit and trial on the elements from now on. :) Figured one out just now. Trying to learn each element as and when I make an error. Thanks for all your help. :) – Sid Jan 16 '17 at 20:51