这个是没问题的代码:用来打开谷歌搜索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:\pcode\24.py", line 9, in <module>
ele.send_keys("test")
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 293, in send_keys
self._execute(Command.SEND_KEYS_TO_ELEMENT, {'value': typing})
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webelement.py",
line 370, in _execute
return self._parent.execute(command, params)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 166, in execute
self.error_handler.check_response(response)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.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()

  

AttributeError: 'WebElement' object has no attribute 'send_keys'的更多相关文章

  1. 关于AttributeError: 'NoneType' object has no attribute 'send_keys'

    在学web自动化测试时,通过PO模型将特定页面的一些元素及元素操作放在特定页面模块中, 然后提取公共的部分, 如元素等待WebDriverWait, 元素操作send_keys, click, 获取元 ...

  2. Python脚本报错AttributeError: ‘module’ object has no attribute’xxx’解决方法

    最近在编写Python脚本过程中遇到一个问题比较奇怪:Python脚本完全正常没问题,但执行总报错"AttributeError: 'module' object has no attrib ...

  3. AttributeError: 'list' object has no attribute 'write_pdf'

    我在可视化决策树,运行以下代码时报错:AttributeError: 'list' object has no attribute 'write_pdf' 我使用的是python3.4 from sk ...

  4. attributeError:'module' object has no attribute ** 解决办法

    写了一个小脚本,执行的时候报错: Traceback (most recent call last): File "F:/test/qrcode.py", line 109, in ...

  5. AttributeError: 'module' object has no attribute 'TornadoAsyncNotifier'

    /*************************************************************************** * AttributeError: 'modu ...

  6. AttributeError: 'dict_values' object has no attribute 'translate'

    /***************************************************************************************** * Attribu ...

  7. python3 AttributeError: 'NoneType' object has no attribute 'split'

    from wsgiref.simple_server import make_server def RunServer(environ, start_response): start_response ...

  8. 对于AttributeError: 'Flask' object has no attribute 'cli'的解决办法

    版权声明:本文为博主原创文章,未经博主允许不得转载. 环境flask-script2.0.5.flask0.10.1 运行官方文档sample 出现问题 c:\kk\flask\examples\fl ...

  9. AttributeError: 'module' object has no attribute 'Thread'

    $ python thread.py starting at: 2015-08-05 00:24:24Traceback (most recent call last):  File "th ...

随机推荐

  1. Lazarus 中文汉字解决方案

    使用Lazarus不得不面对编码问题,尤其中文.Lazarus使用的是UTF8编码,而很多windows程序使用的是ANSI编码,编码问题在此不多说大家可以google去. ANSI数据库与Lazar ...

  2. 为什么使用DLL

    (1) 如果不同的程序使用相同的 DLL,只需将 DLL 在内存中装载一次,这样节省了系统内存.DLL 映射到每个进程(每运行一次应用程序)的专用地址空间中,但它们的代码使用的内存影像程序只在内存中装 ...

  3. servlet中url-pattern之/与/*的区别

  4. 吴裕雄 12-MySQL WHERE 子句

    以下是 SQL SELECT 语句使用 WHERE 子句从数据表中读取数据的通用语法:SELECT field1, field2,...fieldN FROM table_name1, table_n ...

  5. SPSS-判别分析

    判别分析 判别分析是一种有效的对个案进行分类分析的方法.和聚类分析不同的是,判别分析时组别的特征已知. 定义:判别分析先根据已知类别的事物的性质,利用某种技术建立函数式,然后对未知类别的新事物进 行判 ...

  6. 用伪类:after画箭头

    在项目中,经常会用到尖头,尤其是表单中,会有剪头的样式,尽量不要用图片显示.用伪类实现. eg   查看更多 > html: <div class="more"> ...

  7. 2018面向对象程序设计(Java)第9周学习指导及要求

    2018面向对象程序设计(Java)第9周学习指导及要求(2018.10.25-2018.10.28)   学习目标 1.掌握java异常处理技术: 2.了解断言的用法: 3.了解日志的用途: 4.掌 ...

  8. easyui-属性表格-easyui-propertygrid-列名显示英文官方文档无修改方案

    修改值name value值为中文示例步骤: 1.<table 中    class="easyui-propertygrid" 中的data-options属性中加入  c ...

  9. HTTP 协议 详解

    一.HTTP简介 1.HTTP协议,即超文本传输协议(Hypertext transfer protocol).是一种详细规定了浏览器和万维网(WWW = World Wide Web)服务器之间互相 ...

  10. CentOS 6.3开机启动服务及自动联网

    虚拟机设置选择NAT模式,默认情况下,CentOS不是自动连接上网的,要点击右上角有个电脑图标,选择system eth0进行连接, 可以修改开机启动配置只需修改:/etc/sysconfig/net ...