我想通過下面的代碼使用硒網絡驅動程序上傳文件上傳文件:無法使用Selenium網絡驅動器
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
這裏是表單元素的HTML中使用它,我要上傳的文件:
<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="✓" /><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>
現在有趣的事情如果我在上傳的文件中將上述表單填入真實瀏覽器並提交,它會將我帶到url:http://www.andromo.com/projects/276345/gallery_activities/24456/edit。但與硒,它帶我到http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items,一個在真正的瀏覽器(登錄後)的鏈接帶我到一個「抱歉這個頁面不存在」頁面。那麼這裏發生了什麼?我也嘗試過使用HtmlUnit(請參閱我今天發佈的問題this),但它給了我相同的結果。
感謝您的答案,但同樣的效果!我想知道是否存在任何方式在java上傳文件通過瀏覽器自動化形式,與兩個最大的球員HtmlUnit和Selenium webDriver失敗! – rahulserver
如果將驅動程序更改爲'FirefoxDriver',它是否有效? – nilesh
是的!你可以把它作爲答案。你爲我節省了一天!我已經爲此贊成了。但令人驚訝的是,爲什麼htmlunit驅動程序不工作? – rahulserver