【问题标题】:Unable to switch to iframe using Selenium Webdriver无法使用 Selenium Webdriver 切换到 iframe
【发布时间】:2021-09-15 23:12:33
【问题描述】:

我正在尝试学习网页中的拖放功能 Link Section Books 在 iframe 中。 但我无法访问我遇到错误的 iframe

没有这样的元素:无法找到元素:

以下是我尝试过的 Xpath

// WebElement frame =driver.findElement(By.xpath("//div[@id='root']//iframe[@class='st-preview-body']"));
// WebElement frame =driver.findElement(By.tagName("iframe"));
   WebElement frame =driver.findElement(By.xpath("//iframe[@src='https://snippet.webixcode.com/snippet.html?0.0.3']"));
driver.switchTo().frame(frame);

我也尝试使用 by.id 以及使用 id 和类名的 xpath

我会犯什么错误?据我所知,只有一个 iframe 存在

拖放代码。这行得通吗?

WebElement fromdrag=driver.findElement(By.xpath("//span[@class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']"));
WebElement todrop=driver.findElement(By.id("treeTarget"));
Actions act=new Actions(driver);
act.dragAndDrop(fromdrag, todrop).build().perform();

【问题讨论】:

  • 嗨@pamdhu 感谢您的评论。我已经进入框架接下来我想做拖放,下面是我的一段代码''''' WebElement ele1 = driver.findElement(By.xpath("//li[@class='dhx_tree-list__item dhx_tree -list-item']//span[@class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']")); System.out.println(ele1.getText());动作动作=新动作(驱动程序); act.dragAndDropBy(ele1, 448,37).build().perform();我得到 NowindowException 。无法继续拖放。
  • 该操作运行良好,对我来说没有任何错误。请参阅此链接 - darganddrop。并检查NoteThe pixels values change with screen resolution and browser size. This method is hence not reliable and not widely used.

标签: selenium iframe drag-and-drop


【解决方案1】:

它是Nested iframe。我们需要一一切换到那些iframes,才能从All Books访问Elements

页面加载需要时间,最好使用Explicit waits 来查找元素。而且你还需要driver.switchTo().defaultContent();从iframe中出来,在iframe之外找到Elements

public void iframequestion() {
        System.setProperty("webdriver.chrome.driver","C:\\expediaproject\\Chromedriver\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        JavascriptExecutor js = (JavascriptExecutor)driver;
        
        driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
        
        WebElement frame1 = driver.findElement(By.xpath("//iframe[@src='https://snippet.dhtmlx.com/3e0b5r57?mode=mobile']"));
        js.executeScript("arguments[0].scrollIntoView(true);", frame1);
        driver.switchTo().frame(frame1);
        
        WebElement frame2 = driver.findElement(By.xpath("//iframe[@class='st-preview-body']"));
        driver.switchTo().frame(frame2);
        
        WebElement frame3 = driver.findElement(By.id("content"));
        driver.switchTo().frame(frame3);
        
        WebElement ele1 = driver.findElement(By.xpath("//span[text()='Fiction & Fantasy']"));
        System.out.println(ele1.getText());
        
        driver.switchTo().defaultContent();
        
        WebElement ele2 = driver.findElement(By.xpath("//a[text()='View more demos']"));
        System.out.println(ele2.getText());
        driver.quit();
    }
Fiction & Fantasy
View more demos

【讨论】:

  • 嗨@pamdhu 感谢您的评论。我已经进入框架接下来是我想做拖放,
猜你喜欢
  • 2017-05-06
  • 2017-12-24
  • 2023-03-08
  • 2018-12-31
  • 2021-06-19
  • 1970-01-01
  • 1970-01-01
  • 2016-01-15
  • 1970-01-01
相关资源
最近更新 更多