2017-02-11 117 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) 
相关问题