【问题标题】:Switch frame if iframe has same name and id in Selenium [duplicate]如果 iframe 在 Selenium 中具有相同的名称和 ID,则切换框架 [重复]
【发布时间】:2019-04-23 04:31:32
【问题描述】:

我有一个 id 和 name 相同的框架,我想切换框架。我该怎么做?

这是我试过的:

driver.switchTo().frame("iframe");
driver.switchTo().frame("clntcap_frame");
driver.switchTo().frame("//iframe[@id='clntcap_frame']");

HTML 代码

<iframe name="clntcap_frame" id="clntcap_frame" style="" xpath="1"></iframe>

在控制台中:

线程“主”org.openqa.selenium.NoSuchFrameException 中的异常:未按名称或 id iframe 找到框架元素

【问题讨论】:

    标签: selenium


    【解决方案1】:

    首先,确保 iframe 存在于 DOM 中。如果它存在,那么以下应该可以工作 l.

    试试这个: 在此方法中,您尝试传递框架元素本身。

        //To check the element exists or not but avoiding any exception, find all the elements with the id and put them in a list. Since id is a unique identifier, it should return only one.
        List<WebElement> frame = driver.findElements(By.id("clntcap_frame"));
    
    
       //Then check if the list of elements is empty or not. If not, then the frame is present in the DOM
        if(!frame.isEmpty()) {
    
            //Since frame is a list we need to get the element in the index 0
            driver.switchTo().frame(frame.get(0));
        } else {
                System.out.println("Frame with this id doesn't exists");
            }
    

    【讨论】:

    • 这是我得到的:线程“main”中的异常 org.openqa.selenium.NoSuchElementException:没有这样的元素:无法找到元素:{“method”:“xpath”,“selector”:” //iframe[@id='clntcap_frame']"}
    • 我如何检查它是否在 DOM 中?不知道这个请回复
    • @Preeti 更新了代码。现在检查。
    • 这对我有用:new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name")));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-07
    • 2011-06-12
    • 1970-01-01
    • 2018-04-12
    • 1970-01-01
    相关资源
    最近更新 更多