【问题标题】:org.openqa.selenium.NoSuchElementException: no such element (Switch to Frames by Index)org.openqa.selenium.NoSuchElementException:没有这样的元素(按索引切换到帧)
【发布时间】:2022-10-22 03:01:21
【问题描述】:

我正在尝试学习硒,但这一步遇到了麻烦。尝试使用索引查找帧并失败,但是当我尝试使用字符串查找时它成功。那么查找带有索引的帧有什么问题。这是我的代码。

公共类Test3 {

WebDriver driver;

@Test
public void test_3(){

    System.setProperty("webdriver.chrome.driver","drivers/chromedriver.exe");
    driver = new ChromeDriver();
    driver.get("https://demoqa.com/frames");
    driver.manage().window().maximize();


    //driver.switchTo().frame("frame1");
    driver.switchTo().frame(0);


    WebElement frame1 = driver.findElement(By.id("sampleHeading"));
    String text = frame1.getText();
    System.out.println(text);

}

}

错误消息:“org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“css selector”,“selector”:“#sampleHeading”}”

【问题讨论】:

    标签: java selenium selenium-webdriver frames


    【解决方案1】:

    无法找到它,因为在您定位的 iframe 之前还有其他 iframe。您想要的是第三个一帧(2)。您可以使用以下 xpath - //iframe 在开发工具 (F12 > Element > Ctrl + F) 中设置断点并检查页面。您将看到页面中有多少个 iframe。所以最好是按名称或 id 处理 iframe,否则测试很容易失败。

    【讨论】:

    • 谢谢佩特科娃。正如你所说的“//iframe”,当我用这个xpath查看页面时,我看到没有frem(0),它是frame(3)。当然,通过索引搜索更新或更改的帧是不安全的。我不明白为什么我看不到索引框。再次感谢,我应该先检查页面上有多少帧。
    【解决方案2】:

    希望这会奏效

    driver.get("https://demoqa.com/frames"); driver.manage().window().maximize();

        driver.switchTo().frame(driver.findElement(By.id("frame2")));
    
    
        WebElement frame1 = driver.findElement(By.id("sampleHeading"));
        String text = frame1.getText();
        System.out.println(text);
    

    【讨论】:

      猜你喜欢
      • 2013-12-01
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 2019-09-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多