R
R
rinnn2018-07-02 18:38:09
C++ / C#
rinnn, 2018-07-02 18:38:09

How to loop correctly?

private void button1_Click(object sender, EventArgs e)
        {
            IWebDriver driver;
            FirefoxOptions options = new FirefoxOptions();
            options.AddArguments("--headless");
            driver = new FirefoxDriver(options);
            driver.Url = "http://table.a.ru";
            System.Threading.Thread.Sleep(600);
            IWebElement table = driver.FindElement(By.XPath("/html/body/div[2]/table/tbody"));
            string a = ")]";

            string user = textBox1.Text; //введенный в текстбокс id сотрудника, который внедряем в xpath ниже
            IWebElement oper = driver.FindElement(By.XPath("/html/body/div[2]/table/tbody/tr/td[contains(text()," + user + a));
            string dobcount = oper.Text;
            label2.Text = Convert.ToString(dobcount);
            //Сотрудник
            IList<IWebElement> findvalues = driver.FindElements(By.TagName("td"));
            string res = "";

            foreach (IWebElement values in findvalues)
            {
                res += values.Text + "/";
            }
            res = res.Substring(res.IndexOf(dobcount));
            //res = res.Remove(res.IndexOf("tr"));
            String[] td = res.Split(new char[] { '/' });
            res = "Доб. " + td[0] + "\r\n" + " Сотрудник: " + td[1] + "\r\n" + " Пропущенные: " + td[3] + "\r\n" + " Принятые звонки: " + td[5] + "\r\n" + " В перерывах: " + td[14] + "\r\n" + " Текущий статус: " + td[15];
            label3.Text = Convert.ToString(res);
            System.Threading.Thread.Sleep(600);
        }

We receive data from the table, to display the received information in the label, which is located in the window.
The problem is that the first time everything is output, but the second time it gives an error (referring to this line res += values.Text + "/";), which says that the value cannot be found, because the document may have been updated. The table is dynamic, constantly updated, as far as I understand, when the "2nd circle" is going on, the value remains the same as the first time. What can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Satangelus, 2018-07-02
@Satangelus

You yourself write a dynamic label. This means that the size of the findvalues ​​collection can change, and foreach does not like this, since it uses an enumerator, it checks for a change in the collection.
Try the usual for

for (int i = 0; i < findvalues.size(); i++) 
 { 
       res += findvalues[i].Text + "/";
 }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question