【问题标题】:Handling iframes in WebDriver having no ID or Class property在没有 ID 或 Class 属性的 WebDriver 中处理 iframe
【发布时间】:2014-01-08 21:30:19
【问题描述】:

我在自动化具有 iframe 的页面时遇到了问题,

http://jqueryui.com/draggable/

这里的场景是拖放存在于 iframe 元素内的元素,因此 WebDriver 无法识别

【问题讨论】:

  • 您的问题是您无法通过 WebDriver 模拟拖放吗?或者 iframe 没有 id 或类?无论如何,请把问题说清楚一点。
  • xpath 或 CSS 选择器将很容易工作,即使没有 id 或 class 属性,所以这应该不是什么大问题。但是您需要向我们展示来源以获得任何帮助。问题的另一个分支是:stackoverflow.com/questions/14210051/…
  • 贴出你目前尝试过的代码,看看有没有错误。

标签: java selenium


【解决方案1】:

选项 1:使用 CSS 选择器或 XPath

WebElement iframe = driver.findElement(By.cssSelector(".demo-frame"));
// alternative locators:
// XPath: .//iframe[@class='demo-frame']
// use src attribute
// Css Selector: iframe[src*='demos/draggable']
// XPath: .//iframe[contains(@src, 'demos/draggable')]

driver.switchTo().frame(iframe);

WebElement draggable = driver.findElement(By.id("draggable"));
// do your drag, where do you want to drop?

选项2:使用索引(不推荐)

driver.switchTo().frame(0);

【讨论】:

  • 非常感谢您的回答 :) 您如何发现元素的 XPath?我不知道 XPath 是什么,请相应地指导我,我总是在 Sel-IDE 中右键单击并单击任何元素以获取 XPath,但在一般情况下,我们如何找到 XPath?
【解决方案2】:

对于 xpath,您必须在 Firefox 浏览器中通过插件安装 firebug 和 firepath。安装后,firebug 将显示在您的导航栏上。您只需单击它,您就可以在那边找到 firepath,它将在检查员的帮助下为您提供 xpath css 路径等。

【讨论】:

    猜你喜欢
    • 2016-06-14
    • 2021-12-28
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 2019-09-10
    • 2012-08-10
    相关资源
    最近更新 更多