Selenium - 如何在 iframe 上定位元素

2024-01-04

我正在尝试在 iframe 中查找元素。但我无能为力。这是我的代码和我收到的错误。

这是我的脚本:

public class Add_Lists {

    public static void main (String []args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver", Constants.Chrome_Driver);

        WebDriver driver = new ChromeDriver();

        driver.manage().window().maximize();

        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);

        driver.get("http://automation.cloudaccess.host/administrator"); 

        driver.findElement(By.id("mod-login-username")).sendKeys("admin");

        driver.findElement(By.id("mod-login-password")).sendKeys("admin@123");

        driver.findElement(By.id("mod-login-password")).submit();

        driver.findElement(By.linkText("Content")).click();

        Actions action = new Actions(driver);
        action.moveToElement(driver.findElement(By.linkText("Articles"))).build().perform();

        driver.findElement(By.linkText("Add New Article")).click();

        ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");

        driver.findElement(By.linkText("Article")).click();

        Thread.sleep(5000);

        new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'administrator/index.php')]")));
        WebElement filter = driver.findElement(By.id("filter_search"));

        filter.click();

        filter.sendKeys("Test");
    }

}

我收到的错误:

Exception in thread "main" org.openqa.selenium.TimeoutException: Expected condition failed: waiting for frame to be available: By.xpath: //iframe[contains(@src,'administrator/index.php')] (tried for 20 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:81)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:271)
    at testScripts.Add_Lists.main(Add_Lists.java:106)
Caused by: org.openqa.selenium.NoSuchElementException: Cannot locate an element using By.xpath: //iframe[contains(@src,'administrator/index.php')]
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.12.0', revision: '7c6e0b3', time: '2018-05-08T15:15:08.936Z'
System info: host: 'vowellt4', ip: '127.0.1.1', os.name: 'Linux', os.arch: 'amd64', os.version: '4.15.0-24-generic', java.version: '1.8.0_171'
Driver info: driver.version: unknown
    at org.openqa.selenium.support.ui.ExpectedConditions.lambda$findElement$0(ExpectedConditions.java:896)
    at java.util.Optional.orElseThrow(Optional.java:290)
    at org.openqa.selenium.support.ui.ExpectedConditions.findElement(ExpectedConditions.java:895)
    at org.openqa.selenium.support.ui.ExpectedConditions.access$000(ExpectedConditions.java:44)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:517)
    at org.openqa.selenium.support.ui.ExpectedConditions$17.apply(ExpectedConditions.java:513)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:248)
    ... 1 more

我也尝试过添加等待,但没有帮助。单击按钮后会打开 iframe,我希望与 iframe 元素进行交互


你的代码看起来近乎完美。但是,我建议进行以下两项更改:

  • 当你寻找框架可用并切换到它改变src属性如下:

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//iframe[contains(@src,'http://automation.cloudaccess.host/administrator/index.php?')]")));
    
  • 一旦您切换到框架 induce WebDriver等待当你寻找该框架内的元素如下:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//input[@id='filter_search']"))).sendKeys("Test");
    
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Selenium - 如何在 iframe 上定位元素 的相关文章

随机推荐

  • 在 xtext 语法中定义原语

    我想使用 xtext 创建一个极其简单的 DSL 具有以下功能 它将有两种基本类型 数字和字符串 用户可以定义自己的类 类由字段声明组成 字段声明将名称与类型相关联 其中类型可以是类或基元 以下是我对 DSL 的尝试 类定义和引用工作正常
  • UWP 应用程序在启动时自动启动

    一切都在标题中 我目前正在寻找一种方法来启动我的UWP app 自动地仅使用 UWP 框架启动 Windows 时 计算机上没有文件操作 该应用程序必须能够在应用商店中共享并在 Windows 启动时打开 这是一件可行的事情吗 如果是这样怎
  • 组织导入 Eclipse 方法

    I need to know which method is called inside eclipse when I press CTRL SHIFT O Organise Imports in order to invoke it af
  • 使用 azurerm 配置 Terraform 子网时出错

    最近我发现我的 AKS 集群拥有一个太小的子网 因此 我尝试添加第二个子网和节点池 现在可以通过 Azure CNI 实现 然后创建一个适当的子网并将其迁移回来 During terraform plan有效响应一切顺利 但是在应用时会抛出
  • Git 在制表符和空格之间进行转换,但只是有时

    首先 我是 git 的新手 就像 如果缓存在暂存区域中命中我 我几乎无法区分缓存和索引 或类似的东西 排除了这一点 我的问题是这样的 假设我想要处理一个编码风格要求缩进空格的项目 但我喜欢制表符 似乎我可以使用清洁和涂抹功能 但有一个问题
  • 在 JS 中新建一行

    你好 我有这个 JS 代码 function myFunction var g var d new Date getDay switch d case 0 g Today it s Sunday break case 1 g Today i
  • 如何发送 FastAPI 响应而不将用户重定向到另一个页面?

    我正在使用 FastAPI 创建一个 API 它接收form data从 HTML 页面处理数据 需要一些时间 并返回一条消息 说明此任务已完成 这是我的后端 from cgi import test from fastapi import
  • 安装 geopandas 的问题

    我正在尝试在我的笔记本电脑 Windows 10 版本 1709 机器 上安装 geopandas 执行 pip install geopandas 命令后 我收到以下消息 command python setup py egg info
  • 为什么jsp改变时tomcat不需要重启

    我一直在使用JSP Servlet很长一段时间 我知道每当我们改变任何东西Servlet我们需要重新启动 Tomcat 服务器才能获取更改 如果 JSP 发生更改 tomcat 不需要重新启动 据我所知JSP页面被转换成Servlet仅当编
  • 如何检测浏览器中的链接复制情况?

    昨天我和一个出租车司机聊天 当他提到我是一名程序员时 他告诉我 几天前他经历了以下情况 在尝试从浏览器地址栏中复制 URL 时 出现一个消息框带有类似的消息 请不要复制此链接 而是注册 我不是网络开发人员 所以这可能是一个蹩脚的问题 但我想
  • Jenkins 插件中的变量替换

    我正在开发一个新的 Jenkins 插件 该插件将在 Jenkins 作业的构建阶段执行 并且要求允许用户在插件的作业配置中指定变量名称 而不是文字值 目的是 当作业执行时 用户指定的变量名称将被替换为与该变量关联的实际值 并且插件将在运行
  • ASP.NET MVC - 动态样式表

    我想让用户选择网站的背景颜色并将所选颜色保存在数据库中 当用户登录时 后台将显示正确的颜色 基于以下website http www codeproject com KB aspnet CSSVariables aspx 我可以在范围内设置
  • 如何有条件地为张量赋值[屏蔽损失函数]?

    我想创建一个 L2 损失函数 忽略标签值为 0 的值 gt 像素 张量batch 1 包含标签 同时output是净输出的张量 两者的形状均为 None 300 300 1 labels mask tf identity batch 1 l
  • laravel phpexcel 更新中不明确的类解析

    我尝试使用 php excel 更新 laravel 同时安装我在作曲家中发现了以下警告 Error Warning Ambiguous class resolution SettingsController was found in bo
  • 可移植地处理 C++ 中的异常错误

    我正在致力于将 Visual C 应用程序移植到 GCC 应该在 MingW 和 Linux 上构建 现有代码使用 try except 1 在几个地方阻塞 这样几乎没有什么 除了内存不足类型错误 会让程序退出而不做一些最小的日志记录 使用
  • Git merge - 三路“git merge”

    I am trying to simulate the example on three way merge process in git given here https git scm com book en v2 Git Branch
  • Psr7 Http Message,为什么不可变?

    我正在看PSR 7 https github com php fig http message tree master src接口并思考如何实现它们 我也一直在读这篇博文 http evertpot com psr 7 issues 显然
  • Vue.js:v-for完成后的事件

    我正在尝试使用 Vue js 构建一个简单的聊天应用程序 我的问题是 当写入新消息时 消息区域需要滚动到底部 我使用 v for 指令循环遍历消息 v for 更新 DOM 时是否有事件 我已经做到了 以便消息区域 div 监听组件的消息数
  • 时间序列作为 data.table 中的“ts”列?

    我有多组时间序列数据 希望帮助找出将它们放入 R 并使用 R 进行分析的最佳方法 我对 data table 非常熟悉 但对 R 不太熟悉ts支持时间序列分析的类 我特别想知道如何使用ts http stat ethz ch R manua
  • Selenium - 如何在 iframe 上定位元素

    我正在尝试在 iframe 中查找元素 但我无能为力 这是我的代码和我收到的错误 这是我的脚本 public class Add Lists public static void main String args throws Interr