C# &引用;消息=未知错误:无法聚焦元素";通过Selenium、ChromeDriver和Chrome执行测试时

C# &引用;消息=未知错误:无法聚焦元素";通过Selenium、ChromeDriver和Chrome执行测试时,c#,selenium,google-chrome,webdriver,selenium-chromedriver,C#,Selenium,Google Chrome,Webdriver,Selenium Chromedriver,我正试图在google chrome浏览器中发送下拉列表的键,但收到此错误 OpenQA.Selenium.WebDriverException HResult=0x80131500 Message=unknown error: cannot focus element (Session info: chrome=68.0.3440.106) (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3

我正试图在google chrome浏览器中发送下拉列表的键,但收到此错误

OpenQA.Selenium.WebDriverException
  HResult=0x80131500
  Message=unknown error: cannot focus element
  (Session info: chrome=68.0.3440.106)
  (Driver info: chromedriver=2.40.565498 (ea082db3280dd6843ebfb08a625e3eb905c4f5ab),platform=Windows NT 10.0.17134 x86_64)
  Source=WebDriver
  StackTrace:
   at OpenQA.Selenium.Remote.RemoteWebDriver.UnpackAndThrowOnError(Response errorResponse)
   at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebElement.Execute(String commandToExecute, Dictionary`2 parameters)
   at OpenQA.Selenium.Remote.RemoteWebElement.SendKeys(String text)
   at BnI.UITests.Register.TheOfficialInfoValidTest() in C:\Users\me\UITests\Register.cs:line 382
这是我的方法:

driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='Country:'])[1]/following::span[2]")).SendKeys("USA");
这是它在html中的外观:

<div _ngcontent-c4="" class="col-xs-8 no-padding-sides ng-star-inserted" style="">
   <kendo-dropdownlist _ngcontent-c4="" class="custom-dropdown k-widget k-dropdown k-header ng-pristine ng-invalid ng-touched" formcontrolname="countryId" dir="ltr">
      <span role="listbox" unselectable="on" class="k-dropdown-wrap k-state-default" id="k-d0697a91-baeb-4960-bfc1-c023903c1159" dir="ltr" readonly="false" tabindex="0" aria-disabled="false" aria-readonly="false" aria-haspopup="true" aria-expanded="false" aria-owns="dbfc0894-a4f5-4f1b-881f-1d88ccdc6002" aria-activedescendant="f65cbfa2-6280-4a84-908b-ba11da75d59d-undefined">
         <span unselectable="on" class="k-input">
            <!----><!---->Select Country ...
         </span>
         <span unselectable="on" class="k-select"><span class="k-i-arrow-s k-icon"></span></span><!---->
      </span>
      <!----><!---->
   </kendo-dropdownlist>
</div>
如果我想使用selenium IDE单击dropdownlist值,因为该值是从数据库中检索到的,那么这就是dropdownlist值的外观:

  • 首先我点击了下拉列表
  • 第二,我选择了这个国家

  • 在selenium中是否有其他方法可以通过键盘而不是send key来增加值?

    根据您共享的HTML,目标元素是一个
    标记,因此您将无法调用
    SendKeys()
    方法。相反,您需要诱导WebDriverWait使所需元素可单击,并且您可以使用以下任一解决方案:

    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[@class='k-dropdown-wrap k-state-default' and @role='listbox']/span[@class='k-input']"))).Click();
    //or
    new WebDriverWait(driver, TimeSpan.FromSeconds(10)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//span[@class='k-dropdown-wrap k-state-default' and @role='listbox']//span[@class='k-select']"))).Click();
    
    您可以使用操作库来解决此问题。moveToElement允许webdriver聚焦元素。另外,在创建驱动程序名称时,请使用ExpectedCondition创建一个wait方法

    WebDriverWait wait = new WebDriverWait(WebdriverName, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.tagName(locator)));
    

    用相关的HTMLDone更新问题。现在检查,请阅读原因。考虑使用格式化的基于文本的相关HTML、代码试验和错误堆栈跟踪更新问题。现在更好了吗?伟大的工作JAMESFALON,您能发布更多的周围HTML吗?很难根据您的需求分析和测试如此小的html。
    Actions actions = new Actions(WebdriverName);
    actions.moveToElement(locator).click();
    
    WebDriverWait wait = new WebDriverWait(WebdriverName, 10);
    wait.until(ExpectedConditions.elementToBeClickable(By.tagName(locator)));