使用Webdriver使用Selenium进行超时的最佳方法是什么

编程入门 行业动态 更新时间:2024-10-11 05:27:23
本文介绍了使用Webdriver使用Selenium进行超时的最佳方法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到了一个问题.我的网页有一个DropDownList控件. DropDownList值更改后(通过选择其他值),页面将刷新并呈现内容.

I have run into a problem. My web page has a DropDownList control. Once the DropDownList value changes (by selecting a different value), the page refreshes and it renders the contents.

然后我必须先使用Thread.Sleep(2000);和FindElement.

我的问题:等待网页加载的最佳方法是什么?

My question: What is the best way to wait till the page loads?

我的代码中有很多Thread.Sleep(2000)实例,我开始认为这不是解决问题的最佳方法.

I have so many instances of Thread.Sleep(2000) in my code that I am beginning to think this is not the best way to approach the problem.

这是我的代码:

[TestInitialize()] public void Setup() { if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.IE)) { driver = new InternetExplorerDriver(); } else if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.CHROME)) { //driver = new ChromeDriver(); } else if (BaseIntegrationTest.browserType.Equals(BaseIntegrationTest.FIREFOX)) { driver = new FirefoxDriver(); } }

第二部分:

[TestMethod] public void testVerifyData() { // ................... // ................... driver.FindElement(By.XPath("//*[@id='ctl00_NavigationControl1_lnke']")).Click(); Thread.Sleep(2000); //select from the dropdownlist. IWebElement catagory = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter")); SelectElement selectCatagory = new SelectElement(catagory); selectCatagory.SelectByText("Employee"); Thread.Sleep(2000); // ................... // ................... }

推荐答案

Thread.Sleep()是一种非常不鼓励执行等待的方法

Thread.Sleep() is a very discouraged way to implement your waits

硒代码 seleniumhq/docs/04_webdriver_advanced.html

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); IWebElement category = wait.Until<IWebElement>((d) => { return d.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter")); });

这是一个显式等待的示例,其中硒在找到您的元素之前不会执行任何操作

That is an example of an explicit wait where selenium will not execute any actions until your element is found

隐式等待的示例是:

driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10)); IWebElement category = driver.FindElement(By.Id("ctl00_ContentPlaceHolder1_Filter"));

在隐式等待中,驱动程序将等待给定的时间,并在DOM中轮询不存在的任何元素.

In implicit waits the driver will wait for a given amount of time and poll the DOM for any elements that do not exist.

编辑

public WaitForElement(string el_id) { WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10)); IWebElement category = wait.Until<IWebElement>((d) => { return d.FindElement(By.Id(el_id)); }); }

更多推荐

使用Webdriver使用Selenium进行超时的最佳方法是什么

本文发布于:2023-11-27 22:39:37,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639892.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Webdriver   Selenium

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!