量角器找不到元素

时间:2014-01-28 15:21:07

标签: selenium-webdriver selenium-chromedriver protractor

我正在使用plupload来跨浏览器上传文件。我的HTML看起来像是这样

<div id="fileInputs">
 <div id="html5_18fclrb4o1jgqiufhe6hpghk28_container" class="moxie-shim moxie-shim-html5" style="position: absolute; top: 0px; left: 0px; width: 120px; height: 33px; overflow: hidden; z-index: 0;">
  <input id="html5_18fclrb4o1jgqiufhe6hpghk28" type="file" style="font-size: 999px; opacity: 0; position: absolute; top: 0px; left: 0px; width: 100%; height: 100%;" multiple="" accept="">
 </div>
</div>

所有内联样式都是动态生成的,输入类型文件元素由moxi和plupload生成。我无法通过id引用,因为id会在运行中生成并始终不同。我的测试基本上是试图找到这个输入并发送一些密钥来上传文件


var container = element(by.css('.moxie-shim'));
container.find('input').then(function(input){
  input.getInnerHtml().then(function(inner){
    console.log(inner);
   });
  input.sendKeys('/Users/me/Desktop/toUpload.pdf');
})

我得到输入的日志,确认我确实有输入。但这是我不断得到的错误


UnknownError: unknown error: cannot focus element
  (Session info: chrome=32.0.1700.102)
  (Driver info: chromedriver=2.8.241036,platform=Mac OS X 10.9.1 x86_64) (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 36 milliseconds
Build info: version: '2.39.0', revision: 'ff23eac', time: '2013-12-16 16:11:15'
System info: host: 'climboid.local', ip: '10.0.0.3', os.name: 'Mac OS X', os.arch: 'x86_64', os.version: '10.9.1', java.version: '1.6.0_65'
Session ID: d11f5fe29cf2bb6944820646594d7b24
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities [{platform=MAC, acceptSslCerts=true, javascriptEnabled=true, browserName=chrome, chrome={userDataDir=/var/folders/1s/cnypppy93gz6d0tvwbfc2f7h0000gn/T/.org.chromium.Chromium.QcT29B}, rotatable=false, locationContextEnabled=true, version=32.0.1700.102, takesHeapSnapshot=true, cssSelectorsEnabled=true, databaseEnabled=false, handlesAlerts=true, browserConnectionEnabled=false, nativeEvents=true, webStorageEnabled=true, applicationCacheEnabled=false, takesScreenshot=true}]

非常感谢任何帮助。谢谢!

1 个答案:

答案 0 :(得分:1)

我认为你是如何尝试获取元素导致了这个问题。试试以下内容:

var input = $("input[type='file']")
input.sendKeys('/Users/me/Desktop/toUpload.pdf');

或者出于某种原因需要使用容器:

var input = $('.moxie-shim').findElement(by.tagName('input'))
input.sendKeys('/Users/me/Desktop/toUpload.pdf');

请注意,$是css

的findElement的别名