未知错误:无法聚焦元素

时间:2016-02-16 10:41:04

标签: javascript angularjs selenium protractor

这是输入字段代码:

<span ng-if="cell.state != 'edit_mode'" class="ng-scope">
    <span ng-class="{'with-right-padding' : cell.input_type === 'auto_complete'}" class="value-cell ng-binding">10        </span>
    <!-- ngIf: cell.valid === false && server_side_errors_present -->
</span>

我使用的是以下内容:

//click on the field
element(by.xpath("//div[@class='animate']/div/div[2]/span[2]/section/div/div/section/div/div/div/span/section/div[2]/div/div/section/div[2]/section[1]/table/tbody[1]/tr[4]/td[3]/span/span[1]/span")).click();
//enter the value
element(by.xpath("//div[@class='animate']/div/div[2]/span[2]/section/div/div/section/div/div/div/span/section/div[2]/div/div/section/div[2]/section[1]/table/tbody[1]/tr[4]/td[3]/span/span[1]/span")).sendKeys("10");

问题是重播时我的代码点击事件有效,但发送密钥不起作用。

以下是我得到的错误:

Failures:
1) Customer Add a estimate
  Message:
    Failed: unknown error: cannot focus element
      (Session info: chrome=48.0.2564.109)
      (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 26 milliseconds
    Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:15:17'
    System info: host: 'Bala-PC', ip: , os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=C:\Users\Bala\AppData\Local\Temp\scoped_dir4612_146}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=48.0.2564.109, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]
    Session ID: e5f4a8ca6076e1cafbcc366f45680dd1


  Stack:
    UnknownError: unknown error: cannot focus element
      (Session info: chrome=48.0.2564.109)
      (Driver info: chromedriver=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4),platform=Windows NT 6.1 x86_64) (WARNING: The server did not provide any stacktrace information)
    Command duration or timeout: 26 milliseconds
    Build info: version: '2.51.0', revision: '1af067d', time: '2016-02-05 19:15:17'
    System info: host: 'Bala-PC', ip: '192.168.1.22', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_71'
    Driver info: org.openqa.selenium.chrome.ChromeDriver
    Capabilities [{applicationCacheEnabled=false, rotatable=false, mobileEmulationEnabled=false, chrome={chromedriverVersion=2.21.371459 (36d3d07f660ff2bc1bf28a75d1cdabed0983e7c4), userDataDir=C:\Users\Bala\AppData\Local\Temp\scoped_dir4612_146}, takesHeapSnapshot=true, databaseEnabled=false, handlesAlerts=true, hasTouchScreen=false, version=48.0.2564.109, platform=XP, browserConnectionEnabled=false, nativeEvents=true, acceptSslCerts=true, locationContextEnabled=true, webStorageEnabled=true, browserName=chrome, takesScreenshot=true, javascriptEnabled=true, cssSelectorsEnabled=true}]

请有人给我解决方案。

2 个答案:

答案 0 :(得分:2)

var element = driver.findElement(By.xpath("//div[@class='animate']/div/div[2]/span[2]/section/div/div/section/div/div/div/span/section/div[2]/div/div/section/div[2]/section[1]/table/tbody[1]/tr[4]/td[3]/span/span[1]/span"))
driver.executeScript("arguments[0].setAttribute('value', arguments[1])", element, '10');

这可能会成功。这基本上是使用来自selenium webdriver的执行脚本方法...如果这不起作用,我真的建议尝试使用xpath之外的其他东西测试该字段。我认为困难在于关注这个领域

答案 1 :(得分:0)

以上答案使用javascript,并且如果输入或其父项具有需要触发的事件处理程序,则该答案将不起作用,例如如果是提前输入控件。

为了修复它,我们可以使用Selenium Actions:

Actions assigner = new Actions(webdriver);
assigner.moveToElement(webelement);
assigner.click();
assigner.sendKeys("<your value>");
assigner.build().perform();