本文介绍了Selenium webdriver 多线程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 winforms 项目,每当单击按钮时,它都会调用另一个使用 selenium 的类.我正在努力做到这一点,所以我可以运行 1 - 无论我想要运行多少个窗口.所以,我做到了,每当单击按钮时,它都会创建一个新线程并调用一个调用其他类的方法.问题是每当我执行多个窗口时,似乎只有一个窗口在执行操作.所以如果我想要两个窗口来尝试选择一个国家,只有一个会选择一个国家.但是,两个窗口都打开了 url.我真的很困惑如何或为什么会发生这种情况,我在构造函数中初始化了一个新的 webdriver,所以我不明白为什么它似乎没有做任何其他事情.

I have a winforms project that whenever a button is clicked, it calls another class which uses selenium. I am trying to make it so I could run 1 - however many windows I want running. So, I made it so that whenever the button is clicked it makes a new thread and calls a method that calls the other class. The issue is whenever I do multiple windows, it appears that only one window is doing the action. So if I want two windows to try and select a country, only one will select a country. Both windows however, open the url. I am really confused how or why this is happening, I initialize a new webdriver in the constructor so I don't understand why it doesn't seem to do anything else.

using System;
using System.Threading;
using System.Windows.Forms;

namespace MRE
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(new ThreadStart(generate));
            t.IsBackground = true;
            t.Start();
           // if (!InvokeRequired)
           // {
           //     Invoke((Action)generate);
          //  }
        }

        private void generate()
        {

            Class1 generator = new Class1();
            generator.start();
        }
    }
}
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Support.UI;
using System;
using System.Threading;

namespace MRE
{
    class Class1
    {
        public static IWebDriver driver;
        public static WebDriverWait wait;


        public Class1()
        {

            driver = new ChromeDriver();
            wait = new WebDriverWait(driver, TimeSpan.FromSeconds(20));


        }

        public void start()
        {
            try
            {
                driver.Navigate().GoToUrl("your url");

                //Input DOB


                SelectElement oSelect = new SelectElement(driver.FindElement(By.Id("capture-country")));
                Thread.Sleep(2000);
                oSelect.SelectByValue("GBR");
                //click
                wait.Until(ExpectedConditions.ElementToBeClickable(By.Id("dob-field-inactive"))).Click();


                //enter Month
                wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-month"))).SendKeys("01");
                Thread.Sleep(1000);
                //enter Day
                wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-day"))).SendKeys("01");
                Thread.Sleep(1000);
                //enter Year
                wait.Until(ExpectedConditions.ElementIsVisible(By.Name("dob-year"))).SendKeys("2000");


            }

            catch (WebDriverException e)
            {
            }
        }
    }
}

推荐答案

我其实找到了解决这个问题的方法.我没有将 Webdriver 初始化为类变量,而是使它成为 start() 方法的局部变量.它没有显示在我的 MRE 中,但在我的实际类中,我有不同的方法使用驱动程序,所以我这样做了,所以我调用的任何方法的参数都包含 IWebDriver 作为参数.因此,没有一个 webdriver 实例在多个窗口中运行.如果有其他方法可以解决这个问题,请告诉我.

I actually found one way to solve this problem. Instead of initializing the Webdriver as a class variable, I made it so it was a local variable to the start() method. It is not shown in my MRE, but in my actual class I have different methods that use the driver, so I made it so the parameters for any methods I called included IWebDriver as a parameter. Therefore there wasn't one instance of the webdriver running in multiple windows. If there is another way around this please let me know.

这篇关于Selenium webdriver 多线程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:37
查看更多