Question

i'm usinng selenium 2 beta. i'm trying to click button which opens file attachment dialog. but when i click it nothing happens.

<input class="zf" name="Passport" id="PassportUpload" type="file" onclick="return { oRequired : {} }" maxlength="524288"> 


driver.findElement(By.name("Passport")).click();

using just selenium not selenium 2 i can click it easily.

Was it helpful?

Solution

I guess that the issue is only when using Internet Explorer since IE and FF handles the File Input slightly different: in FF you can click on the button or the field to invoke the Open dialog, while in IE you can click on the button or double-click on the field.

WebDriver using native events so it is sending a native mouse click to the File Input control which is translated to the click on the input field.

It was working in Selenium 1 because it is using the JavaScript to fire the events. To make it work in WebDriver you need to invoke the JavaScript:

WebElement upload = driver.findElement(By.name("Passport"));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);

However the code abouve will not in Firefox, so you can use something like:

WebElement upload = driver.findElement(By.name("Passport"));
if (driver instanceof InternetExplorerDriver) {
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
} else {
    upload.click();
}

OTHER TIPS

maybe try following code:

WebElement upload = driver.findElement(By.name("Passport"));
if (driver instanceof InternetExplorerDriver) {
    ((JavascriptExecutor)driver).executeScript("arguments[0].click();", upload);
} else if (driver instanceof FirefoxDriver) {
 ((JavascriptExecutor)driver).executeScript("arguments[0].click;", upload);
}else {
    upload.click();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top