2017-02-11 104 views
1

我試圖在Google搜索字段中插入一個值。 這裏是IPython的代碼:在Selenium中編輯文本字段

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是谷歌搜索輸入字段,我不能與填充值。 有誰知道如何在這個字段中插入值?

回答

1

sendKeys()Java方法。您需要send_keys()

i.send_keys("test") 

另外請注意,有沒有這樣的方法,在PythonsetAttribute()。您可能需要使用

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