我正在尝试优化硒测试,并希望更好地控制轮询间隔,因此我决定使用允许访问定义轮询周期的DefaultWait。
然而,出现了意料之外的问题 - IgnoreExceptionTypes似乎无法正常工作或我正在做错误的事情。它会立即失败,而不是在Timeout到期时失败。
我试过了:
new WebDriverWait(driver, tss).Until(ExpectedConditions.TitleContains("Certificate Error"));
- 对我来说效果很好,但为了精确,我希望民意调查小于500毫秒;
提供不同的例外类型;
我已删除new WebDriverWait
,因此只有DefaultWait
存在,显然,因为人们在混合不同的等待对象时遇到问题,请参阅此处:https://github.com/seleniumhq/selenium-google-code-issue-archive/issues/8062
如果我在第一次wait.nntil之前添加Task.Delay(3000).Wait();
那么抛出异常,那么它不再抛出它(因为页面能够加载并且元素存在),但后续等待失败以同样的方式。
让我知道这可能是什么原因,我错过了什么?
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Diagnostics;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace TestProject1
{
class Program1
{
static void Main(string[] args)
{
// Initialize IWebDriver parameters
var service = InternetExplorerDriverService.CreateDefaultService();
service.LogFile = @"C:\Users\cbfdbfd\Desktop\IE.log";
service.LoggingLevel = InternetExplorerDriverLogLevel.Debug;
// Initialize the IWebDriver
IWebDriver driver = new InternetExplorerDriver(service);
// Initialize the DefaultWait<>
DefaultWait<IWebDriver> wait = new DefaultWait<IWebDriver>(driver);
wait.Timeout = TimeSpan.FromSeconds(50);
wait.PollingInterval = TimeSpan.FromMilliseconds(50);
wait.IgnoreExceptionTypes(typeof(NoSuchElementException));
driver.Navigate().GoToUrl("https://dfbfd/login");
wait.Until<bool>((d) => {
return d.Title.Contains("Certificate Error");
});
driver.Navigate()
.GoToUrl("javascript:document.getElementById('overridelink').click()");
// handle login
var username = driver.FindElement(By.Name("username"));
var password = driver.FindElement(By.Name("password"));
username.Clear(); username.SendKeys("fdbgfbf");
password.Clear(); password.SendKeys("bgfbgfb");
driver.FindElement(By.Id("login-btn")).Click();
//Task.Delay(3000).Wait();
// open supply chain profiles
wait.Until<IWebElement>((d) => {
return d.FindElement(By.LinkText("fgbgfbgfbgf"));
});
driver.FindElement(By.LinkText("gfbgfbgf")).Click();
// close driver
Task.Delay(3000).Wait();
driver.Close();
}
}
}
调试日志:
D 2017-06-29 02:39:33:656 Browser.cpp(513) Not in navigating state
D 2017-06-29 02:39:33:661 server.cc(355) Response: {"sessionId":"50577ecf-b46c-40a4-a2e0-ebb61a618d88","status":0,"value":null}
D 2017-06-29 02:39:33:666 server.cc(281) Command: POST /session/50577ecf-b46c-40a4-a2e0-ebb61a618d88/element {"using":"link text","value":"Supply Chain"}
D 2017-06-29 02:39:33:667 command.cc(36) Raw JSON command: { "name" : "findElement", "locator" : { "sessionid" : "50577ecf-b46c-40a4-a2e0-ebb61a618d88" }, "parameters" : {"using":"link text","value":"Supply Chain"} }
D 2017-06-29 02:39:33:667 IECommandExecutor.cpp(544) No alert handle is found
D 2017-06-29 02:39:33:667 ElementFinder.cpp(60) Using FindElement atom to locate element having linkText = Supply Chain
I 2017-06-29 02:39:33:667 Browser.cpp(130) No child frame focus. Focus is on top-level frame
D 2017-06-29 02:39:33:755 VariantUtilities.cpp(100) Result type is JScriptTypeInfo
W 2017-06-29 02:39:34:014 response.cc(77) Error response has status code 7 and message 'Unable to find element with link text == Supply Chain' message
D 2017-06-29 02:39:34:014 server.cc(355) Response: {"sessionId":"50577ecf-b46c-40a4-a2e0-ebb61a618d88","status":7,"value":{"message":"Unable to find element with link text == Supply Chain"}}
D 2017-06-29 02:39:34:563 ElementRepository.cpp(113) Refreshing managed element cache. Found 0 to remove from cache.
堆栈追踪:
OpenQA.Selenium.NoSuchElementException occurred
HResult=0x80131500
Message=Unable to find element with link text == Supply Chain
Source=WebDriver
StackTrace:
at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(String mechanism, String value)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElementByLinkText(String linkText)
at OpenQA.Selenium.By.<>c__DisplayClass6.<LinkText>b__4(ISearchContext context)
at OpenQA.Selenium.By.FindElement(ISearchContext context)
at OpenQA.Selenium.Remote.RemoteWebDriver.FindElement(By by)
at TestProject1.Program.<>c.<Main>b__0_1(IWebDriver d) in C:\Users\bernam\Documents\Visual Studio 2017\Projects\ConsoleApp1\ConsoleApp1\Program.cs:line 90
at OpenQA.Selenium.Support.UI.DefaultWait`1.Until[TResult](Func`2 condition)
答案 0 :(得分:1)
尝试查找单个元素时,我遇到了同样的问题。最后,我通过使用以下代码解决了该问题;用FindElements替换FindElement并检查是否找到一个元素。
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(timeoutInSeconds));
wait.PollingInterval = TimeSpan.FromSeconds(0.5);
wait.IgnoreExceptionTypes(typeof(NotFoundException), typeof(NoSuchElementException));
wait.Until(drv =>
{
var elementList = drv.FindElements(by);
return (elementList.Count == 1);
});