0

을 사용하여 파일을 업로드 할 수 없습니다 : 파일이 업로드되고 점점되지나는 다음과 같은 코드에 의해 셀레늄 웹 드라이버를 사용하여 파일을 업로드하려고 셀레늄 웹 드라이버

 WebDriver driver = new HtmlUnitDriver(); 

     // And now use this to visit login page 
     driver.get(URL_UPLOADFORM); 

     // Find the text input element by its name 
     WebElement userIDElement = driver.findElement(By.id("user_login")); 
     userIDElement.sendKeys(USER_ID); 

     WebElement passwordElement=driver.findElement(By.id("user_password")); 
     passwordElement.sendKeys(PASSWORD); 

     passwordElement.submit(); 
     // Enter something to search for 
     //element.sendKeys("Cheese!"); 

     // Now submit the form. WebDriver will find the form for us from the element and redirect to the form with file upload page 
     //element.submit(); 

     // Check the title of the page 
     System.out.println("Page title is: " + driver.getTitle()); 
     System.out.println(driver.getCurrentUrl()); 


     WebElement fileUpload=driver.findElement(By.id("gallery_item_photo")); 
     System.out.println(fileUpload.toString()); 
     fileUpload.sendKeys("C:\\Users\\abc\\Pictures\\fileimg.jpg"); 
     WebElement commitButton=driver.findElement(By.name("commit")); 
     System.out.println(commitButton.toString()); 
     commitButton.click(); 

     System.out.println(driver.getCurrentUrl()); 
     driver.quit(); 

출력은 다음과 같습니다

Page title is: New Photo 
http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items/new 
<input id="gallery_item_photo" name="gallery_item[photo]" value="" type="file" /> 
<button class="big button" name="commit" type="submit" value="commit"> 
http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items 
이제 재미있는 일을

<form accept-charset="UTF-8" action="/projects/276345/gallery_activities/24456/gallery_items" class="formtastic gallery_item" enctype="multipart/form-data" id="new_gallery_item" method="post"><div style="margin:0;padding:0;display:inline"><input name="utf8" type="hidden" value="&#x2713;" /><input name="authenticity_token" type="hidden" value="A5lmLTkPubF7RXTrMN7+jNrHUsy0rsfHMW+Rpisjzug=" /></div> 

    <fieldset class="inputs"><ol> 

     <li class="string optional" id="gallery_item_title_input"><label for="gallery_item_title">Title</label><input id="gallery_item_title" maxlength="255" name="gallery_item[title]" type="text" /> 
<p class="inline-hints">What is this a photo of?</p></li> 

     <li class="string optional" id="gallery_item_description_input"><label for="gallery_item_description">Description</label><input id="gallery_item_description" name="gallery_item[description]" type="text" /> 
<p class="inline-hints">Enter a short description of this photo</p></li> 

     <li class="file optional" id="gallery_item_photo_input"><label for="gallery_item_photo">Upload Photo</label><input id="gallery_item_photo" name="gallery_item[photo]" type="file" /> 
<p class="inline-hints">Maximum 1024x1024, 2 MB, .JPG format</p></li> 

     <li class="numeric required" id="gallery_item_position_input"><label for="gallery_item_position">Position<abbr title="required">*</abbr></label><input id="gallery_item_position" name="gallery_item[position]" type="text" value="100" /> 
<p class="inline-hints">Lower numbers appear first in your gallery</p></li> 
</ol></fieldset> 
    <fieldset class="buttons"><ol> 
     <button class="big button" name="commit" type="submit" value="commit">Save Changes</button> 
     <a href="/projects/276345/gallery_activities/24456/edit" class="big button">Cancel</a> 
     <a href="http://support.andromo.com/kb/activities/photo-gallery-activity" class="big button" target="_blank">Help</a> 
</ol></fieldset> 
</form> 

:

다음 폼 요소의 HTML 내가 파일을 업로드하려고하는 사용 위의 양식을 업로드 한 파일이있는 실제 브라우저에서 채우는 경우 제출시 URL이 http://www.andromo.com/projects/276345/gallery_activities/24456/edit이됩니다. 그러나 셀렌과 함께, 그것은 http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items으로 연결됩니다. 링크는 실제 브라우저 (물론 로그인 한 후)에서 "미안 페이지가 존재하지 않습니다"페이지로 연결됩니다. 그럼 여기서 무슨 일이 일어나고있는거야? 나는 HtmlUnit으로도 작업을 시도했다. (오늘 게시 한 this 질문 참조). 그러나 그 결과는 나에게 똑같은 결과를 준다.

답변

1

코드가 올바른 것 같습니다. 파일이 시스템에 있다고 가정합니다. 업로드 할 파일의 절대 경로도 제공합니다. 나에게 양식이 제대로 제출되지 않는 것 같습니다. 그래서 당신의 코멘트에서 대신 click();

WebElement commitButton=driver.findElement(By.name("commit")); 
commitButton.submit(); 

submit();를 사용하는 것이 좋습니다, 그것은 FirefoxDriver()에 대한 submit(); 작품처럼 보인다 HtmlUnitDriver을 위해하지 않습니다. HtmlUnitDriver에 자바 스크립트가 사용 설정되어 있지 않은 것으로 나타났습니다. 문서 here에서, 또한 최신 셀레늄 라이브러리가 있는지 확인

HtmlUnitDriver driver = new HtmlUnitDriver(); 
driver.setJavascriptEnabled(true); 

또는

HtmlUnitDriver driver = new HtmlUnitDriver(true); 

아래에보십시오.

+0

답변 해 주셔서 감사합니다.하지만 동일한 효과! 두 가지 큰 선수 HtmlUnit 및 셀레늄 webDriver 실패와 브라우저 자동화를 통해 양식에 파일을 업로드하는 자바에 어떤 방법이 있는지 궁금해! – rahulserver

+0

드라이버를'FirefoxDriver'로 변경하면 작동합니까? – nilesh

+0

예! 당신은 대답으로 그것을 넣을 수 있습니다. 날 구 했잖아! 나는 이것에 대해 이미 upvote.But 놀랍게도, 왜 htmlunit 드라이버가 작동하지 않았습니까? – rahulserver