【问题标题】:WebDriver - How to locate iframe without idWebDriver - 如何定位没有 id 的 iframe
【发布时间】:2015-06-28 16:31:11
【问题描述】:

我正在尝试切换到 iframe 以定位元素,但我无法找到 iframe,因为它没有 idname

<div id="eyein-modal" style="display: block; position: fixed; width: 100%; height: 100%; top: 0px; left: 0px; bottom: 0px; right: 0px; z-index: 90000000; background-color: rgba(0, 0, 0, 0.6); overflow: auto; opacity: 1;">
<iframe style="display: block; width:90%; height:90%; border: 0px; margin: 2.5% auto; z-index: 90000000; overflow: hidden;" scrolling="no" src="about:blank">
<html>
   <head>
   <body class="">
      <div id="modal">
      <div id="modal-header">
      <div id="header-logo">
      <div id="title-container" class="">
      <a id="view-event" class="button" target="_blank" href="http://www.link.com">view event</a>
      <div id="close-modal" class="close-dark"></div>

close-modal 是我最终需要的元素

【问题讨论】:

  • 你可以尝试通过它的标签找到它,假设只有 1 个 iframe。
  • .. 或者如果您知道多少 iframe 中的哪一个。顺便问一下,你的页面真的有 9000 万层吗?

标签: selenium iframe selenium-webdriver


【解决方案1】:

除了提供框架名称或id,您还可以切换到框架by index(从零开始):

通过(从零开始的)索引选择帧。也就是说,如果一个页面有三个 帧,第一帧将在索引“0”,第二帧在索引“1” 第三个在索引“2”。一旦选择了框架,所有 WebDriver 接口上的后续调用将针对该框架进行。

driver.switchTo().frame(0);  // assuming this is the first frame on the page

或者,您可以通过定位 iframe 来创建 WebElement 实例,例如,通过 CSS 选择器:

WebElement frame = driver.findElement(By.cssSelector("div#eyein-modal iframe"));
driver.switchTo().frame(frame);

另见:

【讨论】:

  • 您读到了 OP 的想法,他将仅使用 java 进行自动化。+1 为此
  • 我使用了另一个具有 id 的 iframe。所以我所做的是“切换到默认内容:driver.switchTo().defaultContent(); 然后我使用 CSS 选择器使用您的解决方案。谢谢
  • @Madhan 是的,由于 OP 之前的问题,它是 java 的可能性很高:)
  • 我被困在这个问题上很长时间了。谢谢!
【解决方案2】:

如果有人在 R 中使用 RSelenium 时遇到了这个小问题,这就是我使用的:

    webElement <- remDr$findElement(using = "css selector", "#dsq-app1")
    remDr$switchToFrame(webElement)
    # note: remDr is the remoteDriver instance that needs to be opened at the beginning

   webElement <- remDr$findElement(using = "id", value = "dsq-app1")
   remDr$switchToFrame(webElement) 

【讨论】:

    【解决方案3】:

    我遇到了这个问题......使用带有 Codeception 的 Webdriver。我的解决方案是运行一个 javascript sn-p 给 iframe 一个名称,然后我可以根据名称切换到它...这里的详细信息:https://stackoverflow.com/a/48123837/1593026

    【讨论】:

      【解决方案4】:

      根据iframe元素的html:

      <iframe style="display: block; width:90%; height:90%; border: 0px; margin: 2.5% auto; z-index: 90000000; overflow: hidden;" scrolling="no" src="about:blank">
      

      很明显,&lt;iframe&gt; 不具备以下任一属性:

      • id
      • 名字
      • 标题
      • src属性的具体值

      在这种情况下要切换到所需的iframe,您可以参考preceeding(sibling) WebElement,您可以使用以下任一@987654324 @:

      • 基于 Java 的解决方案:

        • 使用 xpathfollowing

          WebElement iframe = driver.findElement(By.xpath("//div[@id='eyein-modal']//following::iframe[1]"));
          driver.switchTo().frame(iframe);
          
        • 使用 xpathfollowing-sibling

          WebElement iframe = driver.findElement(By.xpath("//div[@id='eyein-modal']//following-sibling::iframe[1]"));
          driver.switchTo().frame(iframe);
          
        • 使用cssSelector

          WebElement iframe = driver.findElement(By.cssSelector("div#eyein-modal +iframe"));
          driver.switchTo().frame(iframe);
          
      • 基于 Python 的解决方案:

        • 使用 xpathfollowing

          driver.switch_to.frame(driver.find_element(By.XPATH, "//div[@id='eyein-modal']//following::iframe[1]"))
          
        • 使用 xpathfollowing-sibling

          driver.switch_to.frame(driver.find_element(By.XPATH, "//div[@id='eyein-modal']//following-sibling::iframe[1]"))
          
        • 使用cssSelector

          driver.switch_to.frame(driver.find_element(By.CSS_SELECTOR, "div#eyein-modal +iframe"))
          

      参考

      您可以在以下位置找到一些相关讨论:

      【讨论】:

        猜你喜欢
        • 2014-05-30
        • 2014-01-08
        • 1970-01-01
        • 2018-07-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-01
        • 1970-01-01
        相关资源
        最近更新 更多