Selenium GhostDriver/PhantomJS:未使用sendKeys()上载文件/Javascript
我正在使用PhantomJSDriver/GhostDriver运行一个无头脚本,并试图在撰写电子邮件时在gmail上上传一个文件 工具:Selenium和PhantomJS/GhostDriver html代码如下所示:Selenium GhostDriver/PhantomJS:未使用sendKeys()上载文件/Javascript,java,selenium,webdriver,phantomjs,ghostdriver,Java,Selenium,Webdriver,Phantomjs,Ghostdriver,我正在使用PhantomJSDriver/GhostDriver运行一个无头脚本,并试图在撰写电子邮件时在gmail上上传一个文件 工具:Selenium和PhantomJS/GhostDriver html代码如下所示: Click the "Browse" button to select a file. Click "Done" when you're finished.</table><table width=100% cellpadding=2 cellspacing
Click the "Browse" button to select a file. Click "Done" when you're finished.</table><table width=100% cellpadding=2 cellspacing=0 border=0>
<tr><td align=right>1. </td><td>
<input name="file1" type=file size=42><tr><td align=right>2. </td><td>
<input name="file2" type=file size=42><tr><td align=right>3. </td><td>
<input name="file3" type=file size=42><tr><td align=right>4. </td><td>
<input name="file4" type=file size=42><tr>
收到的例外情况:
Exception in thread "main" org.openqa.selenium.ElementNotVisibleException:
{"errorMessage":"InvalidStateError: DOM Exception 11","request":
{"headers":{"Accept-Encoding":"gzip,deflate","Connection":"Keep-Alive",
"Content-Length":"144","Content-Type":"application/json; charset=utf-8",
"Host":"localhost:48470","User-Agent":"Apache-HttpClient/4.3.4 (java 1.5)"},
"httpVersion":"1.1","method":"POST","post":"{\"args\":[],\"script\":
\"document.getElementsByName(\\\"file1\\\") [0].value='D:\\\\ECLIPSE\\\\WORKSPACE\\\\HeadlessTests\\\\.\\\\attachments\\\ \Alan Morales.doc';\"}","url":"/execute","urlParsed":
{"anchor":"","query":"","file":"execute","directory":"/","path" :"/execute","relative":"/execute","port":"","host":"","password":"","user":"" ,"userInfo":"","authority":"",
"protocol":"","source":"/execute","queryKey":{},"chunks": ["execute"]},"urlOriginal":"/session/3db602a0-d517-11e4-bb05- df9882695874/execute"}}
使用sendKeys():
当我使用sendKeys()时,PhantomJS挂断
问题:有没有办法使用Selenium+PhantomJS上传文件?
多谢各位
编辑:
我也试过下面的说法。虽然,该语句没有给出任何错误,但它没有上载文件
(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');");
对于我来说.NET也有同样的问题——上面所有的方法都给出了相同的结果。
wait.until(ExpectedConditions.presenceOfElementLocated(By.name("file1")));
WebElement btnChoseFile = driver.findElement(By.name("file1"));
System.out.println(btnChoseFile.isEnabled());
File file = new File("attachments/Alan Morales.doc");
btnChoseFile.sendKeys(file.getAbsolutePath());
System.out.println("File attached.....");
(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');");