Selenium GhostDriver/PhantomJS: File not getting uploaded using sendKeys() / Javascript

醉酒当歌 提交于 2019-12-06 07:23:18

问题


I am running a headless script using PhantomJSDriver/GhostDriver and trying to upload a file on gmail while composing an email.

Tool: Selenium with PhantomJS/GhostDriver.

The html code is as below:

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>

So far, I have tried the below two methods to upload a file but none of them are working:

Using Javascript:

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");
String script = "document.getElementsByName(\"file1\")[0].value='" + file.getAbsolutePath() + "';";
jsexec.executeScript(script);
System.out.println("File attached.....");

Exception received:

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"}}

Using sendKeys():

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.....");

PhantomJS hangs up when I use sendKeys().

Question: Is there any way to upload a file using Selenium + PhantomJS? Thank you.

EDIT: I have also tried the below statement. Although, the statement does not give any error, but it does not upload the file.

(PhantomJSDriver) driver.executePhantomJS("var page = this; page.uploadFile('input[type=file]', 'path to file');");

来源:https://stackoverflow.com/questions/29315319/selenium-ghostdriver-phantomjs-file-not-getting-uploaded-using-sendkeys-jav

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