C#中的硒-如何导航不同的框架 [英] Selenium in C# - How do I navigate different frames

查看:66
本文介绍了C#中的硒-如何导航不同的框架的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网站具有以下元素:

它具有3个不同的框架,如何将自己导航到所需的框架? 在下面的代码中,通过反复试验,我发现frameIndex = 1允许我找到那些元素(欢迎,配置,乐器等).

It has 3 different frames, how do I navigate myself to the desired frame? In my following code, using trial and errors I found that frameIndex = 1 allows me to find those elements (welcome, config, instruments, etc).

但是此索引号是否始终保持不变?有没有一种更可靠的方式让我知道哪个帧是哪个帧?

But does this index number stay the same all the time? Is there a more reliable way for me to know which frame is which?

[TestClass]
public class Test2
{
    IWebDriver driver;
    string url = "http://10.116.33.6/";

    [TestInitialize]
    public void TestSetup()
    {
        var IEOption = new InternetExplorerOptions();
        var IEService = InternetExplorerDriverService.CreateDefaultService();
        IEOption.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
        IEOption.IgnoreZoomLevel = true;
        IEService.HideCommandPromptWindow = true;

        driver = new InternetExplorerDriver(IEService, IEOption);
        driver.Navigate().GoToUrl(url);

    }

    [TestMethod]
    public void NavigateMenu()
    {
        driver.SwitchTo().Frame(1);

        var welc = driver.FindElement(By.Name("welcome"));
        var conf = driver.FindElement(By.Name("config"))     ;
        var inst = driver.FindElement(By.Name("instruments"));
        var stat = driver.FindElement(By.Name("status"))     ;
        var help = driver.FindElement(By.Name("help"))       ;

        conf.Click();
    }       
}

推荐答案

您实际上可以使用以下方法选择iFrame:-

You can actually select an iFrame using the below methods: -

  • 框架(索引)
  • frame(框架的名称[或]框架的ID)

  • frame(index)
  • frame(Name of Frame [or] Id of the frame)

frame(WebElement frameElement)

frame(WebElement frameElement)

因此,您可以通过传递有关框架的上述任何信息来进行切换. 是的,您需要根据要求的操作每次切换

So you can switch by passing the any above information about the frame. Yes you need to switch everytime according to require action

我们可以看到您的框架具有不同的名称,例如:-topnavigation等.使用框架名称在它们之间进行切换

As we can see your frame have different name like :- top, navigation etc. Use name of the frame to switch between them

示例:-

driver.SwitchTo().Frame("top");

....在框架上执行操作

.... Perform your action on frame

driver.SwitchTo().defaultContent();

driver.SwitchTo().Frame("navigation");

....在框架上执行操作

.... Perform your action on frame

driver.SwitchTo().defaultContent();

希望它会对您有所帮助:)

Hope it will help you :)

这篇关于C#中的硒-如何导航不同的框架的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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