gpt4 book ai didi

java - 无法使用 Selenium Web 驱动程序上传文件

转载 作者:行者123 更新时间:2023-12-01 13:27:07 29 4
gpt4 key购买 nike

我正在尝试通过以下代码使用 selenium Web 驱动程序上传文件:

        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="&#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>

现在有趣的是,如果我在真实的浏览器中填写上面的表单并上传文件,提交后,它会将我带到 url: http://www.andromo.com/projects/276345/gallery_activities/24456/edit 。但是使用 selenium ,它会将我带到 http://www.andromo.com/projects/276345/gallery_activities/24456/gallery_items ,这是一个在真实浏览器中(当然在登录后)的链接,将我带到“抱歉此页面不存在”页面。那么这是怎么回事呢?我也尝试使用 HtmlUnit 进行此操作。(请参阅我今天发布的 this 问题),但它给了我相同的结果。

最佳答案

你的代码对我来说看起来是正确的。我假设该文件存在于您的系统上。您还提供了要上传的文件的绝对路径,这也是正确的。在我看来,表单没有正确提交。所以我建议使用 submit(); 而不是 click();

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

从您的评论来看,似乎 submit(); 适用于 FirefoxDriver() 而不适用于 HtmlUnitDriver。我注意到您的 HtmlUnitDriver 没有启用 Javascript。来自文档here ,尝试下面

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

HtmlUnitDriver driver = new HtmlUnitDriver(true);

还要确保您拥有最新的 Selenium 库。

关于java - 无法使用 Selenium Web 驱动程序上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21757746/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com