元素无法定位问题,求助
求高手看看,元素如下:<input name="txtusername" id="txtusername" data-val="true" datafield="User.UserName" data-val-required="用户名必须输入" type="text" value="" size="15" class="input input-w240h36">代码如下:
#coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://www.greenpass-tc.com/WebMain.htm")
time.sleep(2)
driver.find_element_by_id("txtusername").send_keys("sa")
driver.quit()
执行的时候一直提示找不到这个元素,报错:
Traceback (most recent call last):
File "D:\selenium_study\test.py", line 8, in <module>
driver.find_element_by_id("txtusername").send_keys("sa")
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 269, in find_element_by_id
return self.find_element(by=By.ID, value=id_)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 752, in find_element
'value': value})['value']
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 236, in execute
self.error_handler.check_response(response)
File "D:\Python27\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 192, in check_response
raise exception_class(message, screen, stacktrace)
NoSuchElementException: Message: Unable to locate element: {"method":"id","selector":"txtusername"}
Stacktrace:
at FirefoxDriver.prototype.findElementInternal_ (file:///c:/users/zoumin/appdata/local/temp/tmpmlwwor/extensions/fxdriver@googlecode.com/components/driver-component.js:10770)
at FirefoxDriver.prototype.findElement (file:///c:/users/zoumin/appdata/local/temp/tmpmlwwor/extensions/fxdriver@googlecode.com/components/driver-component.js:10779)
at DelayedCommand.prototype.executeInternal_/h (file:///c:/users/zoumin/appdata/local/temp/tmpmlwwor/extensions/fxdriver@googlecode.com/components/command-processor.js:12661)
at DelayedCommand.prototype.executeInternal_ (file:///c:/users/zoumin/appdata/local/temp/tmpmlwwor/extensions/fxdriver@googlecode.com/components/command-processor.js:12666)
at DelayedCommand.prototype.execute/< (file:///c:/users/zoumin/appdata/local/temp/tmpmlwwor/extensions/fxdriver@googlecode.com/components/command-processor.js:12608)
xpath也试了,也是不行,求高手大大帮忙解答下是什么问题呀
看下整个html代码,这个input会不会在iframe里面?如果在,先切换一下iframe classname 也定位不了? time.sleep(2) 这个单位我记得是毫秒哦。。。你这么设置太短了,等于没设。。把时间调长点。。应该是没加载完成所以没找到吧 #coding=utf-8
from selenium import webdriver
import time
driver = webdriver.Firefox()
driver.get("http://www.greenpass-tc.com/WebMain.htm")
time.sleep(2)
driver.switch_to_frame("maincontent")
driver.find_element_by_id("txtusername").send_keys("sa")
driver.quit() 这种问题 一般先调试 试着先定位此id上一层的id 如果上一层id也找不到 再上一层,出来的路径可能会比较长,调试成功后可以再试着简化路径。
可以多试试其他的定位 classtextname xpath 数列 等等 因为txtusername属性在id为maincontent的iframe中,你可以在txtusername上面的节点中找iframe,所以需要先切换到iframe中 因为这是在iframe里面,所以在driver.find_element_by_id("txtusername").send_keys("sa")之前加上:
driver.switch_to_frame("maincontent")
time.sleep(1)
naiuty13 发表于 2016-7-28 09:29
看下整个html代码,这个input会不会在iframe里面?如果在,先切换一下iframe
谢谢,解决了,是这个问题,感谢感谢 :) 涉及框架的确实要注意
页:
[1]