“Message=unknown error: cannot focus element” while executing tests through Selenium, ChromeDriver and Chrome

情到浓时终转凉″ 提交于 2020-02-25 04:30:10

问题


I am trying to send key for a drop down list in google chrome browser but i am receiving this error

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

This is my method :

driver.FindElement(By.XPath("(.//*[normalize-space(text()) and normalize-space(.)='Country:'])[1]/following::span[2]")).SendKeys("USA");

This how its look in 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>

This is the XPath :

xpath=(.//*[normalize-space(text()) and normalize-space(.)='Country:'])[1]/following::span[2]

This how dropdownlist value look like if i want to click on it using selenium IDE because this value is retrieved from database :

  1. First i clicked on the dropdownlist
  2. Second i selected the country

Is there another way to add value by keyboard in selenium instead of send key ?


回答1:


As per the HTML you have shared the target element is a <span> tag so you won't be able to invoke SendKeys() method. Instead you need to induce WebDriverWait for the desired element to be clickable and you can use either of the following solution:

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();



回答2:


Actions actions = new Actions(WebdriverName);
actions.moveToElement(locator).click();

You can use Actions library to solve issue. moveToElement enables the element to be focused by webdriver. Also when you create driver name make a wait method with ExpectedCondition.

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


来源:https://stackoverflow.com/questions/52036559/message-unknown-error-cannot-focus-element-while-executing-tests-through-sele

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!