【问题标题】:Unable to open File Upload window using Selenium Webdriver无法使用 Selenium Webdriver 打开文件上传窗口
【发布时间】:2014-09-21 07:12:56
【问题描述】:

我是 Selenium Webdriver 的新手,我想在单击浏览按钮后打开文件上传窗口,但我无法使用 webdriver 打开它。

这是我的代码:

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Login_Page {
 static WebDriver driver;
    public static void main(String args[])
    {
        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        WebDriverWait wait = new WebDriverWait(driver, 40);
WebDriverWait wait = new WebDriverWait(driver, 40);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.get("http://www.toolsqa.com/automation-practice-form");

            driver.findElement(By.id("photo")).click();



    }
}

我看不到任何文件上传窗口。

我正在使用 Firefox 14.0.1 和 selenium-server-standalone-2.24.1.jar

请告诉我我该怎么做? 谢谢

【问题讨论】:

  • 添加 HTML 的 sn-p。

标签: java selenium file-upload selenium-webdriver


【解决方案1】:

我猜你想在点击上传按钮后上传文件。即使您可以单击将弹出窗口的上传按钮,您也无法使用 selenium 调用选择文件。

所以为了上传文件,你需要这样做:

WebElement uploadButton = driver.findElement(//your strategy) //to the upload button
uploadButton.sendKeys("your full path to the file")

您还需要为相应的 FireFox 浏览器使用最新的 Selenium 版本。

【讨论】:

  • 点击上传按钮后没有弹出窗口,这就是我要问的问题,无法打开上传窗口。
  • 另外请告诉我哪个 firefox 版本和哪个 selenium 版本最好?
【解决方案2】:

问题在于您的 Firefox 版本。在我的这个脚本运行顺利。一旦我遇到这样的问题,这个脚本只会让浏览按钮成为焦点。你可以做的是在获得焦点后,发送回车键。 在点击事件后添加这段代码。

Actions action = new Actions(driver);
action.sendKeys(Keys.ENTER);

【讨论】:

  • 您使用的是哪个 firefox 版本和哪个 selenium-server-standalone 文件?
  • Firefox 13.XX 和 Selenium-2.42.2.jars
【解决方案3】:

使用下面的行:

driver.findElement(By.xpath(.//*[@id='photo']).click();

【讨论】:

  • 请考虑正确格式化你的答案,添加一些细节,避免发布这样简短的答案,最好有解释清楚的答案。