【发布时间】:2018-10-22 05:43:48
【问题描述】:
我正在将 Selenium 的 ChromeDriver 与 Python 一起使用,我试图在我的页面上找到一个具有以下 HTML 的按钮:
<input id="j_id0:SiteTemplate:j_id255:new" type="submit" name="j_id0:SiteTemplate:j_id255:new" value="New" class="kbutton-white">
我知道唯一不变的是 id 和 name 以“new”结尾,我正在尝试使用以下代码来识别并单击该元素:
test_runner.driver.find_element_by_css_selector('[id*=new]').click()
但是,当我运行代码时出现此错误:
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[id*=new]"}
我的错误是什么?
更新:此元素位于 iframe 中,我必须先切换到 iframe,然后才能尝试找到该元素。答案请看 cmets。
【问题讨论】:
-
我正在关注selenium-python.readthedocs.io/locating-elements.html 使用 Python 定位元素,但不确定我是否可以在那里使用建议的解决方案?
-
确保当您运行该语句时该元素出现在页面上,并且其 ID 是您提到的值。
<input>可能是动态添加的,或者它的 ID 发生了变化。 -
1- 我尝试使用 ("[id$=new]") 作为另一个问题的已接受答案中的建议并得到相同的错误。 2- @Titus 我可以确认上面提到的 id 和 name 在那里。但是,我知道 是动态添加的。它是如何改变的?
-
@Jorjani ,那么你需要switch to iframe来处理里面的元素
标签: python selenium selenium-webdriver css-selectors webdriver