Selenium webdriver 多线程
Selenium webdriver multiple threads
我有一个 winforms 项目,只要单击一个按钮,它就会调用另一个使用 selenium 的 class。我正在努力做到这一点,所以我可以 运行 1 - 不管我想要多少 windows 运行ning。因此,我这样做是为了在单击按钮时创建一个新线程并调用一个调用另一个 class 的方法。问题是每当我执行多个 windows 时,似乎只有一个 window 在执行操作。所以如果我要两个windows尝试和select一个国家,只有一个会select一个国家。既windows不过,打开url。我真的很困惑这是怎么发生的或者为什么会发生,我在构造函数中初始化了一个新的 webdriver,所以我不明白为什么它似乎没有做任何其他事情。
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 初始化为 class 变量,而是让它成为 start()
方法的局部变量。它没有显示在我的 MRE 中,但在我的实际 class 中,我有使用驱动程序的不同方法,所以我做了它,所以我调用的任何方法的参数都包含 IWebDriver
作为参数。因此,在多个 windows 中没有一个 webdriver 运行 的实例。如果有其他解决方法,请告诉我。
我有一个 winforms 项目,只要单击一个按钮,它就会调用另一个使用 selenium 的 class。我正在努力做到这一点,所以我可以 运行 1 - 不管我想要多少 windows 运行ning。因此,我这样做是为了在单击按钮时创建一个新线程并调用一个调用另一个 class 的方法。问题是每当我执行多个 windows 时,似乎只有一个 window 在执行操作。所以如果我要两个windows尝试和select一个国家,只有一个会select一个国家。既windows不过,打开url。我真的很困惑这是怎么发生的或者为什么会发生,我在构造函数中初始化了一个新的 webdriver,所以我不明白为什么它似乎没有做任何其他事情。
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 初始化为 class 变量,而是让它成为 start()
方法的局部变量。它没有显示在我的 MRE 中,但在我的实际 class 中,我有使用驱动程序的不同方法,所以我做了它,所以我调用的任何方法的参数都包含 IWebDriver
作为参数。因此,在多个 windows 中没有一个 webdriver 运行 的实例。如果有其他解决方法,请告诉我。