【问题标题】:Selenium not finding elements elements after an iFrame tagSelenium 在 iF​​rame 标记之后找不到元素元素
【发布时间】:2019-09-12 13:33:32
【问题描述】:

我正在尝试在某些 HTML 中查找搜索框,但 selenium 找不到它。

我尝试了以下方法。

driver.find_element_by_id
driver.find_element_by_class_name
driver.find_element_by_xpath
driver.find_element_by_css_selector

到目前为止没有任何效果。

我还逐行浏览了 HTML 并找到了它停止拾取元素的点,在它可以找到的最后一个和它找不到的第一个之间是一个文本字符串,如下所示. (删除,否则我无法发布)

iframe id="iframeCenter" src="url initialise=true" width="100%" 
onload="resizePortlet()" frameborder="0" height="894"
/iframe

#document

!-- tabbedFindMaintenanceLayout.jsp --
html class="dj_quirks ... "/html etc

出于安全原因,显示 src="URL 的部分已被缩短,但它只是我激活的网页的 url

搜索按钮位于此 HTML 标记内的更下方,但我的驱动程序无法到达那里。

这里是搜索框的html

tr

td class="form_inputBoxTitle"><input type="text" name="search" size="20" 
value="" id="form_search"/td

input type="text" name="search" size="20" value="" id="form_search"

/tr

现在是我的代码:/

查找搜索框

search_box = driver.find_element_by_class_name('form_inputBoxTitle')
search_box.send_keys(phc)

查找 iframe

iFrame = driver.find_element_by_id('iframeCenter')

寻找html类

dj = driver.find_element_by_id('dj_quirks ')

iFrame 会按照我的预期找到正确的 html 标记。 search_box 和 dj 都返回以下错误信息。

search_box = driver.find_element_by_class_name('form_inputBoxTitle')
  File "C:\Users\webdriver.py", line 564, in find_element_by_class_name
    return self.find_element(by=By.CLASS_NAME, value=name)
  File "C:\Users\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such 
element: Unable to locate element: {"method":"css 
selector","selector":".form_inputBoxTitle"}
  (Session info: chrome=76.0.3809.132)

这里有什么问题?

【问题讨论】:

  • 您为什么不尝试将密钥发送到您的输入?只需为您的输入创建一个自定义 id,然后执行 find_element_by_id
  • 我试过使用发送键,但我找不到要使用的元素。它也不是我的网站,所以我无法更改 html
  • 还有一个有趣的注意事项,我已经加载了页面并在加载框之前打开了 html,即使在框出现之后,html 也丢失了。
  • 如果你想在 iframe 中搜索,你应该切换到 iframe - 就像这里描述的那样:stackoverflow.com/questions/7534622/… 此外,selenium 找不到还没有的东西,在等待的过程中实现一些等待内容。
  • 这是巨大的进步。我已经进行了等待,但使用 driver.switch_to 让我走得更远。然而,不完全是搜索字段

标签: python html selenium-chromedriver


【解决方案1】:

试试这个:

search_box = driver.find_element_by_id("form_search")
search_box.send_keys("foo")

【讨论】:

  • 你是说 driver.find_element_by_id("form_search") 吗?
  • 是的,对不起。编辑我的帖子。
猜你喜欢
  • 2020-05-29
  • 2019-01-02
  • 2021-03-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多