是否可以在Selenium Webdriver Java中使用driver.switchTo()。frame(" frameName")而切换到框架中的元素? [英] Is it possible to switch to an element in a frame without using driver.switchTo().frame("frameName") in Selenium Webdriver Java?

查看:919
本文介绍了是否可以在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")));
    


  • 这篇关于是否可以在Selenium Webdriver Java中使用driver.switchTo()。frame(&quot; frameName&quot;)而切换到框架中的元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    相关文章
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆