Running loop in selenium without crashing the form c #

0

Good afternoon friends, I'm developing whatsapp marketing automation software using selenium and C # language.

The software is already functional and functional, however, I want to perfect it ... basically it has the function of sending messages from a textbox to each number of a listview through a for.

My question is: how to run this for without crashing the main window, so that I can put a pause button and continue.

I've read all about await, task, thread, async, but I can not apply to my code in any way.

The code of a button I want to execute without crashing the form.

I'm new to C #.

 private void button1_Click_1(object sender, EventArgs e)
    {
        int contaErro;
        contaErro = 0;
        int contaAcerto;
        contaAcerto = 0;
        if (textBox4.Text != "") // verifica se tem foto/video selecionada
        {
            if (listView1.Items.Count > 0) // verifica se tem NUMERO NA LISTA
            {
                if (textBox3.Text != "") // verifica se tem mensagem escrita
                {
                    if (driver.FindElements(By.XPath("//input[@class='jN-F5 copyable-text selectable-text']")).Count() != 0) // verifica se ja logou no whatsapp
                    {
                        for (int i = 0; i < listView1.Items.Count; i++)
                        {
                            IWebElement inptPesquisa = driver.FindElement(By.XPath("//input[@class='jN-F5 copyable-text selectable-text']")); // caixa de pesquisa
                            inptPesquisa.Click();
                            inptPesquisa.Clear();
                            inptPesquisa.SendKeys(listView1.Items[i].Text);
                            System.Threading.Thread.Sleep(1000); // tempo para carregar contato


                            if (driver.FindElements(By.XPath("//div[@class='_2wP_Y']")).Count() != 0) // verifica se o numero esta na lista
                            {
                                IWebElement selContato = driver.FindElement(By.XPath("//div[@class='_2wP_Y']")); // se tiver seleciona
                                selContato.Click();
                            }
                            else
                            {

                                contaErro += 1; // se nao conta na lista de erros
                                label4.Text = Convert.ToString(contaErro);
                                label4.Update();
                                continue; // pula para o proximo numero
                            }
                            label8.Text = Convert.ToString(listView1.Items[i].Text); // lê numero atual antes de enviar msg
                            label8.Update(); // informa o numero para a label

                            System.Threading.Thread.Sleep(200);
                            IWebElement inptAnexo = driver.FindElement(By.XPath("//div[contains(@role, 'button') and contains(@title, 'Anexar')]"));
                            inptAnexo.Click();
                            System.Threading.Thread.Sleep(200);

                            IWebElement inpFile = driver.FindElement(By.XPath("//input[@type='file']"));    // carrega a imagem                
                            inpFile.SendKeys(textBox4.Text);

                            System.Threading.Thread.Sleep(1000);
                            IWebElement cxTxt = driver.FindElement(By.XPath("//div[@class='_2S1VP copyable-text selectable-text']"));    // carrega a imagem                
                            cxTxt.Click();
                            cxTxt.Clear();
                            cxTxt.SendKeys(textBox3.Text);

                            System.Threading.Thread.Sleep(200);
                            IWebElement btnEnv = driver.FindElement(By.XPath("//div[contains(@role, 'button') and contains(@class, '_3hV1n yavlE')]"));
                            btnEnv.Click();

                            contaAcerto += 1; // se tudo deu ok conta acerto
                            label6.Text = Convert.ToString(contaAcerto); // escreve na label
                            label6.Update(); // atualiza ela

                            System.Threading.Thread.Sleep(Int32.Parse(textBox1.Text)); // delay entre mensagens


                        }
                    }


                    else
                    {
                        MessageBox.Show("FAÇA LOGIN NO WHATSAPP WEB", "CARREGUE O QRCODE",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("ESCREVA UMA MENSAGEM", "Erro",
                        MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("ADICIONE UM NUMERO A LISTA", "Erro",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        else
        {
            MessageBox.Show("ADICIONE UMA FOTO/VIDEO", "Erro",
                       MessageBoxButtons.OK, MessageBoxIcon.Error);
        }
    }

I've spent a lot of time doing this kk software but it's already working, so anyone who needs automation whatsapp just call.

    
asked by anonymous 07.10.2018 / 22:02

0 answers