我在自动化具有 iframe 的页面时遇到了问题,
这里的场景是拖放 iframe 元素内部存在的元素,因此 WebDriver 无法识别
最佳答案
选项 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);
关于java - 在 WebDriver 中处理没有 ID 或 Class 属性的 iframe,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20696512/