在Selenium

时间:2017-11-21 00:27:27

标签: selenium for-loop selenium-webdriver bots

我正在制作一个应该每15分钟刷新一次交易的脚本。它运作良好,但后来我发现了一个问题。当它发现必须刷新的交易时。

else if (text[2] == "hours" || text[2] == "hour" || text[2] == "days" || 
text[2] == "day" || text[2] == "month" || text[2] == "months" || x >= 15)

它进入交易,编辑它并保存它应该。但就在此之后它就会停止工作。它说该程序仍在运行,但不再发生任何事情。我不知道为什么它不起作用,因为它适用于这种情况:

if (text[2] == "seconds")
                continue;

我找不到循环停止工作的原因。非常感谢任何想法或提示。

代码:

    private void CheckItems(IWebDriver driver)
    {
        IList<IWebElement>
            trades = driver.FindElements(By.ClassName("rlg-trade-display-header")); // add all trades to list

        for (var i = 0; i <= trades.Count; i++) // loop to check all trades
        {
            driver.Navigate().GoToUrl(tradeUrl);
            if (i == 0)
                Console.WriteLine("Active Trades: " + trades.Count);
            var text = driver
                .FindElement(By.XPath(
                    "/html/body/main/div/div/div/div[4]/div[" + (i + 1) + "]/div[1]/div/div/span"))
                .Text.Split(); // reads text from trade and adjust it for the program
            Console.WriteLine("Trade " + (i + 1) + " was last updated " + text[1] + " " + text[2] + " ago.");

            var x = int.Parse(text[1]); // convert string to int to check time later

            if (text[2] == "seconds")
                continue;


            if (text[2] == "hours" || text[2] == "hour" || text[2] == "days" || text[2] == "day" ||
                text[2] == "month" || text[2] == "months" || x >= 15)
            {
                var element1 =
                    driver.FindElement(By.XPath("/html/body/main/div/div/div/div[4]/div[" + (i + 1) +
                                                "]/div[1]/a/div"));
                var actions1 = new Actions(driver);
                actions1.MoveToElement(element1).Click().Perform(); // scrolls down to choosen element and clicks it


                driver.FindElement(By.XPath("/html/body/main/div/div/div/div[2]/a[1]"))
                    .Click(); // click choosen element


                var element = driver.FindElement(By.Name("btnSubmit"));
                var actions = new Actions(driver);
                actions.MoveToElement(element).Click().Perform(); // scrolls down to choosen element and clicks it
                Thread.Sleep(2000);

                Console.ReadLine();
            }
        }
    }

    private static void Main(string[] args)
    {
        var bot = new Program();
        bot.setup(bot.driver);

        while (true)
        {
            bot.CheckItems(bot.driver);


            Thread.Sleep(15000); // wait 15 second before next loop
        }
    }

我发现当我在Console.ReadLine()之后放入bot.CheckItems(bot.driver);时,它现在将刷新所有交易并打印一个整体的所有内容进行循环然后停止并且不会继续。如果我把它保持在代码中我给予更高,它可以完美地工作,如果所有交易都不需要刷新,但即使是一个,它将刷新它并停止工作。任何想法可能是这个问题的原因?为什么Console.ReadLine()很重要?

0 个答案:

没有答案