Andy
Andy

Reputation: 1092

Edit text fields in Selenium

I am trying to insert a value into the Google Search field. Here is the code in IPython:

    In [1]: from selenium import webdriver

    In [2]: driver=webdriver.Chrome("/ChromeDriver/chromedriver")

    In [3]: driver.get("https://www.google.com/")

    In [4]: i=driver.find_elements_by_id("lst-ib")[0]

    In [5]: i.click()

    In [6]: i.sendKeys("test")
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-6-dcc9ebf0e928> in <module>()
    ----> 1 i.sendKeys("test")

    AttributeError: 'WebElement' object has no attribute 'sendKeys'

    In [7]: i.setAttribute("value","test")
    ---------------------------------------------------------------------------
    AttributeError                            Traceback (most recent call last)
    <ipython-input-7-cf9217d1c7f8> in <module>()
    ----> 1 i.setAttribute("value","test")

    AttributeError: 'WebElement' object has no attribute 'setAttribute'

i is the Google Search input field, which I can't fill with values. Does anybody know how to insert values into this field?

Upvotes: 1

Views: 860

Answers (1)

Andersson
Andersson

Reputation: 52665

sendKeys() is Java method. You need send_keys():

i.send_keys("test")

Also note that there is no such method as setAttribute() in Python. You might need to use

driver.execute_script('arguments[0].setAttribute("value", "test")', i)

Upvotes: 1

Related Questions