尝试使用Selenium Python时出现NoSuchElementException
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了尝试使用Selenium Python时出现NoSuchElementException,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3412字,纯文字阅读大概需要5分钟。
内容图文
![尝试使用Selenium Python时出现NoSuchElementException](/upload/InfoBanner/zyjiaocheng/696/ffe0879139e54208bca287e67e0924bd.jpg)
尝试使用Selenium在python中查找元素时,我不断收到NoSuchElementException.我正在等待页面完全加载,并且正在切换到正确的框架(或者至少我认为是!).
这是代码:
driver.get("https://www.arcgis.com/home/signin.html")
driver.implicitly_wait(10)
driver.switch_to_frame("oAuthFrame")
elem = driver.find_element_by_name('username')
elem1 = driver.find_element_by_name('password')
这是我要访问的网页部分:
<input id="user_username" class="textBox" type="text" name="username" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false">
位于里面
<iframe dojoattachpoint="_iFrame" id="oAuthFrame" scrolling="no" style="display: block; border: 0px;" marginheight="0" marginwidth="0" frameborder="0" width="400" height="500"...>
您可以在https://www.arcgis.com/home/signin.html查看自己的源代码
完整错误输出:
Traceback (most recent call last):
File "C:\Python34\beginSample.py", line 12, in <module>
elem = driver.find_element_by_name('username')
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 302, in find_element_by_name
return self.find_element(by=By.NAME, value=name)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 662, in find_element
{'using': by, 'value': value})['value']
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\webdriver.py", l
ine 173, in execute
self.error_handler.check_response(response)
File "C:\Python34\lib\site-packages\selenium\webdriver\remote\errorhandler.py"
, line 164, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: 'no such element\n
(Session info: chrome=35.0.1916.153)\n (Driver info: chromedriver=2.9.248315,pl
atform=Windows NT 6.1 SP1 x86_64)'
如果有人可以帮助我找出问题所在,我将不胜感激.
更新:我现在正在使用操作,并且已经调试到没有错误的地步,但是也没有输入任何内容.这是代码:
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.send_keys("sd")
actions.send_keys(Keys.TAB)
actions.send_keys("bg")
actions.perform()
解决方法:
我个人尝试使用xpath并利用xquery功能.您仍然可以使用其他附件,因为它们通常是元素的“属性”,但是它为包含搜索的复杂类型提供了更大的灵活性.我能够找到具有以下两个附加xpath的用户名/密码字段.您不需要先切换到iframe …尽管某些直接调用(例如name)似乎不起作用… xpath版本对我有用.
element = driver.find_element_by_xpath("//input[@name='username']")
element1 = driver.find_element_by_xpath("//input[@name='password']")
更新:
我能够复制该问题…
我不确定为什么定位器没有专门为此工作,但是这里有一个解决方法.加载页面时焦点的默认位置是用户名框.
actions = selenium.webdriver.common.action_chains.ActionChains(driver)
actions.SendKeys("sd").Perform()
actions.SendKeys(selenium.webdriver.common.Keys.Tab).Perform()
actions.SendKeys("bg").Perform()
http://selenium.googlecode.com/git/docs/api/py/webdriver/selenium.webdriver.common.action_chains.html
如果可以找出直接连接到字段的方法,我将再次更新.
更新:使用Chrome浏览器,如果您使用特定的iFrame,则会看到一条错误消息,提示您的浏览器不支持此iFrame.我认为通讯故障可能是由于浏览器无法正确读取…即使它似乎可以呈现并允许功能正常.由于Selenium是基于DOM选择的,因此它将尝试利用类似的行为来找到它.我建议尝试使用driver.execute_script(“脚本在这里”)并尝试仅使用javascript定位它,看看是否可行.然后,您当然必须继续使用javascript,并直接使用javascript修改控件属性/属性,然后为该按钮执行javascript提交事件.
内容总结
以上是互联网集市为您收集整理的尝试使用Selenium Python时出现NoSuchElementException全部内容,希望文章能够帮你解决尝试使用Selenium Python时出现NoSuchElementException所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。