Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
线程中的Selenium Web驱动程序问题。C#_C#_Multithreading_Selenium - Fatal编程技术网

线程中的Selenium Web驱动程序问题。C#

线程中的Selenium Web驱动程序问题。C#,c#,multithreading,selenium,C#,Multithreading,Selenium,这是密码 browser = new FirefoxDriver(); browser.Navigate().GoToUrl("https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry"); Thread.Sleep(5000); browser.FindEleme

这是密码

browser = new FirefoxDriver();
browser.Navigate().GoToUrl("https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry");
Thread.Sleep(5000);
browser.FindElement(By.Name("ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShown")).SendKeys("asdf");

它工作正常,但若我在线程中运行,它会显示元素不可见。。。。为什么会抛出线程?

元素可能不可见,因为页面在检查时没有重新加载,或者网站使用了动态名称、类等

您可以尝试以下方法:

IWebDriver browser = new FirefoxDriver();
browser.Navigate().GoToUrl("https://www.vicroads.vic.gov.au/registration/buy-sell-or-transfer-a-vehicle/buy-a-vehicle/check-vehicle-registration/vehicle-registration-enquiry");

while ( true ) {
    try {
        browser.FindElement(By.Name("ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShown")).SendKeys("asdf"); 
        break;
    }
    catch { Thread.Sleep(1000);}
}

根据您尝试的
xpath
,似乎
name
属性是动态的。要找到注册号的文本框,可以尝试以下选项之一:

  • css选择器

    browser.FindElement(By.CssSelector("input[class=text text xlong v_registrationNumber v_required][id^=ph_pagebody_)]")).SendKeys("asdf");
    
    browser.FindElement(By.XPath("//input[@class='text text xlong v_registrationNumber v_required'][starts-with(@id, 'ph_pagebody_')]")).SendKeys("asdf");
    
  • XPath

    browser.FindElement(By.CssSelector("input[class=text text xlong v_registrationNumber v_required][id^=ph_pagebody_)]")).SendKeys("asdf");
    
    browser.FindElement(By.XPath("//input[@class='text text xlong v_registrationNumber v_required'][starts-with(@id, 'ph_pagebody_')]")).SendKeys("asdf");
    

你们能详细说明一下你们所说的吗
若我在线程中运行,它会显示不可见的元素
线程的任何特定配置
?我正在运行一个线程,在该线程中导航URL,然后访问元素。当我试图访问元素时,它会说“元素不可见”。但是,如果我在没有线程的情况下运行代码,它可以正常工作。如何在没有线程的情况下运行它???请澄清您的具体问题或添加其他详细信息,以突出显示您所需要的内容。正如目前所写的,很难准确地说出你在问什么。请参阅本页以获取澄清此问题的帮助。无法工作,兄弟。我告诉过你,我的代码在没有线程的情况下运行得很好,但在线程中,它显示出异常。你可以尝试使用Task,mb,它可能会有帮助。我告诉过你,我的代码在没有线程的情况下运行良好,但在线程中它显示异常–你说的
不工作是什么意思?你看到什么错误了吗?错误说明了什么?你能用错误堆栈跟踪更新问题吗?这行“browser.FindElement(By.Name”(“ph_pagebody_0$phthreecolumnmaincontent_1$panel$VehicleSearch$RegistrationNumberCar$RegistrationNumber_CtrlHolderDivShowed”)。SendKeys(“asdf”)工作正常并返回元素。但当我在线程中运行此代码时,它找不到元素。@user1448783您是否尝试过我的答案?你能告诉我结果吗?