【问题标题】:Selenium Webdriver C# VS: script failing with NoSuchElementExceptionSelenium Webdriver C# VS:脚本因 NoSuchElementException 而失败
【发布时间】:2016-08-20 00:37:56
【问题描述】:
我正在尝试我的脚本并不断收到此错误消息。我不知道为什么,因为我的 xpath 是正确的。我什至尝试更改它几次,但仍然收到相同的错误消息...有人对此有解决方案吗?
WebDriver.dll 中出现“OpenQA.Selenium.NoSuchElementException”类型的异常,但未在用户代码中处理
附加信息:没有这样的元素:无法找到元素:{"method":"xpath","selector":"//input[@id='Password']"}
(会话信息:chrome=52.0.2743.116)
(驱动程序信息:chromedriver=2.22.397933 (1cab651507b88dec79b2b2a22d1943c01833cc1b),platform=Windows NT 10.0.10586 x86_64)
【问题讨论】:
标签:
c#
selenium
webdriver
【解决方案1】:
如果您收到NoSuchElementException,可能有以下原因:-
-
可能当您要查找元素时,它不会出现在DOM 上,因此您应该执行WebDriverWait 以等待元素可见,如下所示:-
IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Password")));
-
这个元素可能在任何frame 或iframe 中。如果是,您需要在找到以下元素之前切换 frame 或 iframe :-
IWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until(ExpectedConditions.FrameToBeAvailableAndSwitchToIt("frame name or id"));
IWebElement element = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("Password")));