C#-在任务中发布复制/粘贴
我最近开始喜欢使用任务而不是创建线程并加入它。它解决了一些问题,同时运行多个单线程方法。下面是方法1(我的旧版本)和方法2(测试当前版本) (见下文) 方法1:C#-在任务中发布复制/粘贴,c#,html-agility-pack,selenium-chromedriver,C#,Html Agility Pack,Selenium Chromedriver,我最近开始喜欢使用任务而不是创建线程并加入它。它解决了一些问题,同时运行多个单线程方法。下面是方法1(我的旧版本)和方法2(测试当前版本) (见下文) 方法1: if (comboBox1.SelectedItem.ToString() == "123" && comboBox2.SelectedItem.ToString() == "XYZ") { Thread dgnsi = new Thread(DG_NSI); // Kick off a new
if (comboBox1.SelectedItem.ToString() == "123" && comboBox2.SelectedItem.ToString() == "XYZ")
{
Thread dgnsi = new Thread(DG_NSI); // Kick off a new thread
dgnsi.SetApartmentState(ApartmentState.STA);
dgnsi.IsBackground = false;
b_run.Enabled = true;
dgnsi.Start();
}
方法2:
if (comboBox1.SelectedItem.ToString() == "123" && comboBox2.SelectedItem.ToString() == "XYZ")
{
Task dgnsi = Task.Factory.StartNew(() =>
{
DG_NSI();
});
}
线程开始:
[STAThread]
private void DG_NSI()
{
var timespan = TimeSpan.FromMinutes(4);
var driverService = ChromeDriverService.CreateDefaultService();
driverService.HideCommandPromptWindow = true;
using (IWebDriver driver = new ChromeDriver(driverService, new ChromeOptions()))
{
driver.Manage().Timeouts().SetPageLoadTimeout(timespan);
string _pa = tb_payagreement.Text;
string _noa = noa;
string _si = si;
//Add Pay Agreement
IWebElement pa_I = driver.FindElement(By.XPath("/html/body/form/table[2]/tbody/tr/td/table/tbody/tr[@id='tab'][2]/td/table/tbody/tr[3]/td/table/tbody/tr[2]/td/table[@class='Ntexteleftalignnowrapnoborder']/tbody/tr[2]/td[1]/textarea[@class='textarea']"));
pa_I.Click();
Clipboard.Clear(); // Always clear the clipboard first
Clipboard.SetText(_pa);
SendKeys.SendWait("^v");
//Add Nature of activity
IWebElement noa_I = driver.FindElement(By.Name("woNOA"));
noa_I.Click();
Clipboard.Clear(); // Always clear the clipboard first
Clipboard.SetText(_noa);
SendKeys.SendWait("^v");
//Add Special Instructions
IWebElement si_I = driver.FindElement(By.Name("woSI"));
si_I.Click();
Clipboard.Clear(); // Always clear the clipboard first
Clipboard.SetText(_si);
SendKeys.SendWait("^v");
//Close Application
driver.Quit();
}
} //Complete
为了解释我试图实现的目标,我试图通过点击Winforms按钮来使用selenium为自己自动化任务。目前我有多个线程设置与上面相同。从方法1->方法2开始,我遇到的一个问题是,在复制/粘贴字符串中的信息并粘贴之前,一切都很正常。它根本不会粘贴任何东西,并且一路完成任务。有人知道如何将信息粘贴到任务或备选方案中吗
(注意:我尝试过sendkeys,但速度太慢。这也是一个简化版的代码。)所有重复的剪贴板代码可能都应该放在一个方法中,该方法使用
ChromeDriver
、string
、和By
参数……谢谢,我回到家后会尝试一下@Rufus L在另一个方法中尝试以下内容。我对使用不同的方法清除冗余相当陌生。但是,此方法不起作用,仍然跳过了粘贴过程publicvoid CopyPaste(字符串复制,IWebElement元素){Clipboard.Clear();//始终先清除剪贴板。SetText(复制);元素。单击();SendKeys.SendWait(^v”);}