且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

隐式等待命令不起作用-Selenium WebDriver C#

更新时间:2022-12-15 12:37:11

根据文档隐含等待是要告诉 WebDriver 在尝试 HTML DOM >或find all elements()(如果它们不立即可用).但是 DOM树中元素的可用性不能保证

As per the documentation, an Implicit Wait is to tell the WebDriver to poll the HTML DOM for a certain amount of time when trying to find an element() or find all elements() if they are not immediately available. But availability of an element in the DOM Tree doesn't guarantees that the ElementToBeClickable as you have tried in your code block. Hence you face the exception as Element not found.

因此,解决您的问题的方法是诱使 WebDriverWait ExpectedConditions 子句为 ElementToBeClickable 不仅会确认可用性 HTML DOM 中元素的元素,但还要确保 Element是Clickable 的,即 Element可以显示并启用,如下所示:

So the solution to your issue is to induce Explicit Wait i.e. WebDriverWait with ExpectedConditions clause as ElementToBeClickable which will not only confirm the availability of an element in the HTML DOM but also ensure that the Element is Clickable i.e. Element is Displayed and Enabled as follows:

WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("loginlink")));