如何避免将 Thread.Sleep 与 C# Selenium 2.44 Chrome 驱动程序一起使用
本文关键字:Chrome 驱动程序 一起 Selenium 何避免 Thread Sleep | 更新日期: 2023-09-27 18:34:54
以下是我的开发环境的详细信息:
- Visual Studio 2012 Ultimate with Update 4
-谷歌浏览器版本 38.0.2125.111 m
- Windows 7 专业版与 32 位操作系统
-硒.NET 2.44.0
-努尼特
使用Selenium 2.44.0的Firefox驱动程序,自动化UI可以快速正确地工作。 此外,我不必使用任何Thread.Sleep和使用Selenium 2.44.0的Firefox驱动程序的C#代码。
使用 Selenium 2.44.0 的 Firefox 驱动程序的示例 C# 代码
IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)drvArg).ExecuteScript("return document.readyState").Equals("complete"));
String find1stLnkXpath = "//div[@class='jarviswidget jarviswidget-sortable']/descendant::div[@role='content']/descendant::div[@id='grid-container']/descendant::table[@class='k-selectable' and @role='grid']/tbody/tr[1]/td/descendant::a[@class='k-button k-button-icontext k-grid-view']";
Thread.Sleep(threadSleepTimeArg);
wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
wait.Until(drv => isClickable(drv, By.XPath(find1stLnkXpath)));
wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
// k-button k-button-icontext k-grid-view
// class="k-selectable" role="grid"
IWebElement viewCustDetlnk = wait.Until(ElementIsClickable(By.XPath(find1stLnkXpath)));
viewCustDetlnk.Click();
可悲的是,Selenium 2.44.0的Chrome驱动程序(Chromedriver Win32 2.12(运行缓慢,我被迫将Thread.Sleep与使用Selenium 2.44.0的Chrome驱动程序的C#代码一起使用,以确保自动化UI正常工作。
使用Selenium 2.44.0的Chrome驱动程序的示例C#代码(Chromedriver Win32 2.12(
IWait<IWebDriver> wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(driver1 => ((IJavaScriptExecutor)drvArg).ExecuteScript("return document.readyState").Equals("complete"));
String find1stLnkXpath = "//div[@class='jarviswidget jarviswidget-sortable']/descendant::div[@role='content']/descendant::div[@id='grid-container']/descendant::table[@class='k-selectable' and @role='grid']/tbody/tr[1]/td/descendant::a[@class='k-button k-button-icontext k-grid-view']";
Thread.Sleep(3000);
wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
wait.Until(drv => isClickable(drv, By.XPath(find1stLnkXpath)));
wait.Until(drv => (true == Exists(drv, By.XPath(find1stLnkXpath))));
IWebElement viewCustDetlnk = wait.Until(ElementIsClickable(By.XPath(find1stLnkXpath)));
viewCustDetlnk.Click();
请告诉我如何修改Selenium 2.44.0的Chrome驱动程序(Chromedriver Win32 2.12(的代码,以便我可以避免使用Thread.Sleep
在我们应用程序的 GUI 中,我们有一个div html 元素,它表现为一个不可见的加载屏幕,我们需要等待它不再存在在我们单击按钮之前。对于使用 Selenium 2.44.0 的 Firefox 驱动程序的测试代码,Wait 对象可以等到不可见 在代码尝试查找按钮对象之前,加载屏幕不再存在。
奇怪的是,使用Selenium 2.44.0的Chrome驱动程序(Chromedriver Win32 2.12(的测试代码需要使用Wait对象来等待,直到不可见 在我的代码尝试单击按钮对象之前,加载屏幕不存在:
wait.Until(drv => ! Exists(drv, By.CssSelector("div.loading-screen");
btn.Click();
Selenium 2.44.0的Chrome驱动程序(Chromedriver Win32 2.12(似乎仍然存在一些小问题。