小工具知识网

当前位置: 首页 > 行业知识 > C# Selenium Edge 驱动下的常见用法

行业知识

C# Selenium Edge 驱动下的常见用法

2026-12-02 17:22:00 来源:互联网转载

using OpenQA.Selenium;

()

using OpenQA.Selenium.Edge;

using OpenQA.Selenium.Support.UI;

()

//添加缩放属性  将浏览器缩放设为100%  

EdgeOptions options=new EdgeOptions();

options.AddArgument("force-device-scale-factor=1");

//不需添加额外属性 options可不写  直接 var driver = new EdgeDriver()

using (var driver = new EdgeDriver(options))

{

    //打开指定Url路径

    driver.Navigate().GoToUrl("https://test.com");

//将浏览器全屏化

    driver.Manage().Window.Maximize();

    

    try

    {

      /可添加等待时间  等待网页加载  直至某元素出现

        WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

        //等待直到标题出现

        //wait.Until(d=>d.Title.Contains("Example Domain"));

        //等待特定元素出现

        //等待Test按钮出现

        wait.Until(d => d.FindElement(By.Id("test-button")).Displayed);

        //查找按钮元素并点击

        IWebElement buttonElement = driver.FindElement(By.Id("test-button"));

        buttonElement.Click();

        //网页跳转后  等待网页加载

        System.Threading.Thread.Sleep(1000);

        //直到某元素出现  by -name

        wait.Until(d => d.FindElement(By.Name("username")).Displayed);

         //by -css

        //wait.Until(d => d.FindElement(By.CssSelector("input[class='test']")).Displayed);

        // wait.Until(d => d.FindElement(By.XPath("//input[@name='test 1  color 1']")).Displayed);

        //获取class包含test的元素

        IWebElement input_name = driver.FindElement(By.CssSelector("input[class*='test']"));

        //往input框赋值

        input_name.SendKeys("test");

       

        //不好获取ID、Name、Class时,可采用XPath方式

        wait.Until(d => d.FindElement(By.XPath("//button[@type='submit']")).Displayed);

        //wait.Until(d => d.FindElement(By.CssSelector("input[type='submit']")).Displayed);

        //wait.Until(d => d.FindElement(By.ClassName("test")));

        buttonElement = driver.FindElement(By.XPath("//button[@type='submit']"));

      

        buttonElement.Click();

  //Radiobutton 的获取

        //wait.Until(d => d.FindElement(By.XPath("//input[@type='type1' and @name='name' and @value='1']")).Displayed);

        //IWebElement radio_name = driver.FindElement(By.XPath("//input[@type='type' and @name='name' and @value='1']"));

       // radio_name.Click();

     //By Class  获取按钮

        wait.Until(d => d.FindElement(By.ClassName("test-button")).Displayed);

        

        buttonElement = driver.FindElement(By.ClassName("test-button"));

        buttonElement.Click();

        //By XPath  获取a 、i 标签

        //wait.Until(d => d.FindElement(By.XPath("//i[@class='tets test-color-green test-status-ok-core']")).Displayed);

     

        //wait.Until(d => d.FindElement(By.XPath("//a[@Test-click='redirectToselectedTest(TestStatusSystem.id)']")).Displayed);

        IWebElement test_p = driver.FindElement(By.Id("test_p"));

        //获取test_p中的所有p

        //var p_count = test_p.FindElements(By.TagName("p"));

       //只获取test_p里最外层p

        var p_count = sys_p.FindElements(By.XPath("./p"));

       

//获取Strong元素的外层a标签方法

       

        wait.Until(d => d.FindElement(By.XPath("//strong[@id='test']/parent::a")).Displayed);

        buttonElement = driver.FindElement(By.XPath("//strong[@id='test']/parent::a"));

        buttonElement.Click();

      

        //执行JavaScript脚本来滚动页面

        IJavaScriptExecutor js = (IJavaScriptExecutor)driver;

        js.ExecuteScript("window.scrollTo(0,document.body.scrollHeight)");

        

        //下拉到指定标签位置 如: label translate="test"

        IWebElement targetElement = driver.FindElement(By.XPath("//label[@test']"));

        js.ExecuteScript("arguments[0].scrollIntoView(true);", targetElement);

      //勾选CheckBox

        //wait.Until(d => d.FindElement(By.Id("cb_ck")).Displayed);

        //IWebElement checkbox = driver.FindElement(By.Id("cb_ck"));

        //if (checkbox.Selected == false)

        //{

        //    checkbox.Click();

        //}

       

       

    }

    catch(Exception ex)

    {

        MessageBox.Show(ex.Message);

        

    }

    

}

上一篇:电脑安装ae教程

下一篇:忍者怎么找国服服务器?