您好我正在使这段代码工作,我需要帮助。 HTML代码是
<span id="banner">Rolling in 10.00...</span>
这&#34;在10.00滚动......&#34;是一个innerText,10.00是一个计时器,这意味着它将一直运行到0.我有兴趣尝试在计时器为20.00时运行代码。例子
<span id="banner">Rolling in 20.00...</span>
然而,我遇到了很多麻烦。这是一些有效但有套接字异常错误(http://puu.sh/mubug/c33d8501bf.png)的代码
public void waitroller2()
{
Console.WriteLine("start roller timer");
WebDriverWait wait = new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromSeconds(1000));
Stopwatch watch = new Stopwatch();
IWebElement banner = wait.Until(ExpectedConditions.ElementIsVisible(By.Id("banner")));
watch.Start();
do
{
if (banner.Text.Equals("Rolling in 20.00..."))
{
Console.WriteLine("roller ended");
return;
}
} while (watch.Elapsed.Seconds < 1000);
throw new NoSuchElementException();
}
此方法根本不起作用
public void waitroller1()
{
Console.WriteLine("waitroller");
new WebDriverWait(PropertiesCollection.driver, TimeSpan.FromMinutes(1.5)).Until(ExpectedConditions.TextToBePresentInElement(rolling,"Rolling in 20.00..."));
Console.WriteLine("roller end");
}
我认为主要问题是当我长时间使用这个webdriverwait时,它无法正常运行套接字问题。请帮助,任何人都可以教我如何做到这一点
答案 0 :(得分:0)
试试这个。
try
{
string timeToWait = new String(driver.FindElement(By.Id("banner")).Text.ToCharArray().Where(c => Char.IsDigit(c)).ToArray()).Substring(0,2);
int ttw = Convert.ToInt32(timeToWait);
System.Threading.Thread.Sleep(ttw * 1000);
}
catch(Exception ex)
{}
答案 1 :(得分:0)
这就是你所需要的。
IWebElement banner = driver.FindElement(By.Id("banner")).Text;
bool status = false;
int tryTimes = 15;
do
{
if (banner.Text.Equals("Rolling in 20.00..."))
{
Console.WriteLine("roller ended");
status = true;
}
else
{
System.Threading.Thread.Sleep(1000);
banner = driver.FindElement(By.Id("banner")).Text;
tryTimes--;
}
} while (!status && tryTimes > 0);