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

这个是没问题的代码:用来打开谷歌搜索cheese并退出

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0

# Create a new instance of the Firefox driver
driver = webdriver.Firefox()

# go to the google home page
driver.get("http://www.google.com")

# the page is ajaxy so the title is originally this:
print driver.title

# find the element that's name attribute is q (the google search box)
inputElement = driver.find_element_by_name("q")

# type in the search
inputElement.send_keys("cheese!")

# submit the form (although google automatically searches now without submitting)
inputElement.submit()

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(driver, 10).until(EC.title_contains("cheese!"))

    # You should see "cheese! - Google Search"
    print driver.title

finally:
    driver.quit()

  

有问题的代码:打开百度首页 搜索并退出


from selenium import webdriver driver=webdriver.Firefox() driver.get("http://www.baidu.com") element = driver.find_element_by_id("kw1") element.send_keys('test') #这里报错 driver.close()

  错误:AttributeError: 'WebElement' object has no attribute 'send_keys'

看提示sendKeys 没有导入包,

目前代码:

from selenium import webdriver
from selenium.webdriver.remote.webelement import WebElement 
from selenium.webdriver.common.action_chains import ActionChains

driver=webdriver.Firefox()
driver.get("http://www.baidu.com")
ele = driver.find_element_by_name("wd")
print (ele)
ele.send_keys("test")

btn = driver.findElement(By.id("su1"));
btn.click()

driver.close()

报错提示:

Traceback (most recent call last):
File "D:pcode24.py", line 9, in <module>
ele.send_keys("test")
File "D:Python27libsite-packagesseleniumwebdriver emotewebelement.py",
line 293, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "D:Python27libsite-packagesseleniumwebdriver emotewebelement.py",
line 370, in _execute
return self._parent.execute(command, params)
File "D:Python27libsite-packagesseleniumwebdriver emotewebdriver.py", l
ine 166, in execute
self.error_handler.check_response(response)
File "D:Python27libsite-packagesseleniumwebdriver emoteerrorhandler.py"
, line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not
currently visible and so may not be interacted with' ; Stacktrace:

正确代码如下:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import time

# Create a new instance of the Firefox driver
browser = webdriver.Firefox()

# open baidu.com
browser.get("http://www.baidu.com")

# sleep 2 secs
time.sleep(2)

#clean the enter text
browser.find_element_by_id("kw1").clear()

#enter something
browser.find_element_by_id("kw1").send_keys("selenium")

#submit
browser.find_element_by_id("su1").click()

# sleep 2 secs
time.sleep(2)

try:
    # we have to wait for the page to refresh, the last thing that seems to be updated is the title
    WebDriverWait(browser, 10).until(EC.title_contains("selenium"))

    # You should see "selenium - 百度搜索"
    print browser.title

finally:
    browser.quit()

  

 另外一种写法:

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
import time

# Create a new instance of the Firefox driver
browser = webdriver.Firefox()

# open baidu.com
browser.get("http://www.baidu.com")

# sleep 2 secs
time.sleep(3)

#clean the enter text
browser.find_element_by_id("kw1").clear()

#enter something
browser.find_element_by_id("kw1").send_keys("selenium")

#submit
browser.find_element_by_id("su1").click()

# sleep 2 secs
time.sleep(2)

#wait for the page
while True:
	#if fresh
	contains = browser.title.find("selenium") >= 0
	if (contains):
		break
	else:
		sleep(1)
#quit
browser.quit()

  

原文地址:https://www.cnblogs.com/xiaobaichuangtianxia/p/3714375.html