硒C#不等待

时间:2019-12-17 16:32:58

标签: c# selenium automation selenium-chromedriver

我正在研究一个ASP.NET MVC学院项目,该项目的某些部分需要自动化在codeforces.com上提交问题的过程 问题是当硒不等到元素出现时才出现错误,这样我就报错了:

[ERROR] Runtime.ImportModuleError: Unable to import module 'handler': No module named 'numpy'

,并且在调试时一切正常,没有任何错误。 我已经尝试了很多方法来解决它,但最终所有方法都以该错误或类似错误结尾。

我使用的一些方法: * 等待,直至 *使用虚拟动作等待最大化和最小化 *系统睡眠

这是我的代码

import numpy as np

有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

我认为您的等待在这里不够“明确”-您需要在wait.Until类上调用ExpectedConditions,而不仅仅是在driver.FindElement()上返回元素。

专门用于C#的

ExpectedConditions现在包含在外部名称空间中-不建议使用标准的Selenium绑定。您将需要DotNetSeleniumExtras.WaitHelpers NuGet软件包才能正常工作。我将提供不需要其他依赖项的替代解决方案。

使用DotNetSeleniumExtras.WaitHelpersExpectedConditions

WebDriverWait wait = new WebDriverWait(chromeDriver, TimeSpan.FromMinutes(1));

IWebElement element = wait.Until(SeleniumExtras.WaitHelpers.ExpectedConditions.ElementToBeClickable(chromeDriver.FindElement(By.XPath("//a[@href='/problemset']"))));

不使用任何外部依赖项,调用IWebElement.IsDisplayed()属性:

WebDriverWait wait = new WebDriverWait(chromeDriver, TimeSpan.FromMinutes(1));

var isDisplayed = wait.Until(chromeDriver => chromeDriver.FindElement(By.XPath("//a[@href='/problemset']")).Displayed);

chromeDriver.FindElement(By.XPath("//a[@href='/problemset']")).Click();