是否可以在Selenium Webdriver Java中使用driver.switchTo()。frame(" frameName")而切换到框架中的元素?

是否可以在Selenium Webdriver Java中使用driver.switchTo()。frame(" frameName")而切换到框架中的元素?

问题描述:

我有多个嵌套框架,我需要访问这些框架下的元素。由于这些帧是动态的,我无法访问这些元素。
我是否可以在不使用driver.switchTo()的情况下访问元素.frame()

I have a multiple nested frames and I need to access the elements under these. As these frames are dynamic I am not able to access these elements. Is it possible for me to access the elements without using driver.switchTo().frame()

喜欢使用xpath直接或jquery,javascript或任何东西?
欢迎任何建议

Like using the xpath directly or jquery, javascript or anything? Any suggestions are welcome

简单来说,

,如果不切换到预期的 < iframe> ,就无法访​​问这些元素即不使用 driver.switchTo()。frame()

No, it wouldn't be possible to access the elements without switching into the intended <iframe> i.e. without using driver.switchTo().frame()

切换到您必须使用以下任一项的预定框架:

To switch to the intended frame you have to use either of the following :


  • 切换框架名称

driver.switchTo().frame("frame_name");


  • 切换相框ID

    driver.switchTo().frame("frame_id");
    


  • 切换帧索引

    driver.switchTo().frame(1);
    


  • 切换 WebElement

    driver.switchTo().frame(driver.findElement(By.xpath("//iframe[@attribute='value']")));
    


  • 切换到父框架

    driver.switchTo().parentFrame();
    


  • 切换到默认内容

    driver.switchTo().defaultContent();
    


  • 但按照最佳做法你应该总是诱导 WebDriverWait 以获得所需的框架可用并切换到它,如下所示:

    But as per best practices you should always induce WebDriverWait for the desired Frame To Be Available And Switch To It as follows:


    • 切换框架名称

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("frame_name")));
    


  • 切换相框ID

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.id("frame_id")));
    


  • 切换 Frame cssSelector

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.cssSelector("frame_cssSelector")));
    


  • 切换 Frame xpath

    new WebDriverWait(driver, 20).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("frame_xpath")));