简体   繁体   English

C#+ Selenium:自动化等待

[英]C# + Selenium: automation wait

The below code is not working and it always throws No such element exception at line 2. 下面的代码不起作用,并且始终在第2行没有引发此类元素异常。

wait.IgnoreExceptionTypes(typeof(NoSuchElementException));      
wait.Until(ExpectedConditions.ElementToBeClickable(By.XPath(element)));

There could be 2 issues here: 这里可能有2个问题:

  1. You are trying to find the element before its visible for that you can wait for the element by doing 您正在尝试在元素可见之前找到它,您可以通过执行以下操作来等待该元素

     wait.Until(ExpectedConditions.ElementExists(By.XPath(element))); 

    where element is the XPath of the element you are trying to find. 其中element是您要查找的元素的XPath。

  2. You are not finding the element using the correct XPath. 您找不到使用正确的XPath的元素。 If you are using an absolute XPath, avoid doing because while absolute XPath can find the element faster, if the DOM structure changes your path may no longer work. 如果使用绝对XPath,请避免这样做,因为绝对XPath可以更快地找到元素,但是如果DOM结构更改,则您的路径可能不再起作用。

It is also possible that you are not running your browser in fullscreen, at least this was a valid issue I was facing when my current projects' GUI got changed over. 也有可能您没有以全屏模式运行浏览器,至少这是我当前项目的GUI转换后我所面临的有效问题。 Adding driver.Manage().Window.Maximize(); 添加driver.Manage().Window.Maximize(); to my ClassInitialize fixed the issue in a whim. 我的ClassInitialize立刻解决了这个问题。

Another option is that maybe your element is either embedded into an iframe or is overlapped by one. 另一种选择是,您的元素可能嵌入到iframe中,或者被一个元素重叠。

As mentioned in this answer https://stackoverflow.com/a/44724688/6045154 , I have solved a similar issue with: 如这个答案https://stackoverflow.com/a/44724688/6045154所述 ,我已经解决了类似的问题:

IWebElement button = driver.FindElement(By.ClassName("transfer__button")); IJavaScriptExecutor executor = (IJavaScriptExecutor)driver; executor.ExecuteScript("arguments[0].click();", button);

Of course this needs to be edited to find your element by the right selector. 当然,需要通过正确的选择器对其进行编辑以找到您的元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM