Search code examples
pythonseleniumwebdriverphantomjswebautomation

How do I populate a <p> tag with text in Selenium Webdriver using PhantomJS?


I have an input form that I need to populate with text. It's a div and it has a child node that is a

tag that needs to be populated with text in order to submit the form.

I've tried send_keys on the div itself to no avail, and in my browser I selected the

tag and changed it's TextContent property which resulted in the message box being filled with the text, so I know the

tag has to be filled, but using send_keys on it does not work:

textbox = driver.find_elements_by_xpath(".//div[@role='textbox']/p[1]")[0]
print(textbox)
//<selenium.webdriver.remote.webelement.WebElement (session="a0712590-511d-11e6-8e12-dbe0d5eb709e", element=":wdc:1469309865349")>

Now with send_keys:

   textbox = driver.find_elements_by_xpath(".//div[@role='textbox']/p[1]")[0]
   textbox.send_keys("This is a test")
//selenium.common.exceptions.WebDriverException: Message: Error Message => 
''undefined' is not an object (evaluating 'a.value.length')'       

My question is, how can I enter text input into this text box?


Solution

  • send_keys() works only on that element which needs to be set value on their value attribute means input and textarea, but here you are trying to set value on p element which need to be set on their textContent, So here you should try using execute_script() as below :-

    textbox = driver.find_element_by_xpath(".//div[@role='textbox']/p[1]")
    driver.execute_script("arguments[0].textContent = arguments[1];", textbox, "This is a test")
    

    Or

    textbox = driver.find_elements_by_xpath(".//div[@role='textbox']/p[1]")[0]
    driver.execute_script("arguments[0].textContent = arguments[1];", textbox, "This is a test")
    

    Hope it helps...:)