2014-09-21 66 views
0

我是Selenium Webdriver的新手,我想在点击浏览按钮后打开文件上传窗口,但我无法使用webdriver打开它。无法使用Selenium 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和硒的服务器独立-2.24.1.jar

请让我知道我该怎么办呢? 谢谢

+0

添加HTML片段 – olyv 2014-09-21 20:13:02

回答

0

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

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

其中火狐版本和您正在使用的硒服务器独立文件? – 2014-09-21 07:37:46

+0

Firefox 13.XX和Selenium-2.42.2.jars – 2014-09-21 10:28:42

3

我想你想在点击上传按钮后上传文件。即使您可以点击上传按钮,这会弹出窗口,但您无法使用硒调用选择文件。

所以为了要上传的文件,你需要做的是:

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

,你也需要使用最新版本的硒为您相应的FireFox浏览器。

+0

点击上传按钮后,我没有弹出窗口,这就是我所问的,无法打开上传窗口 – 2014-09-22 02:06:42

+0

也请让我知道哪个版本的硒版本是最好的? – 2014-09-22 02:07:29

0

使用下面行:。

driver.findElement(By.xpath(.//*[@ ID = '照片'])点击();

+2

请考虑格式化你的答案,给它添加一些细节,避免发布答案与这样的短内容,它总是有更好的解释答案。 – 2015-10-06 13:23:54

相关问题