2014-09-21 135 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

相關問題