开发者

cant click button which opens file attachment dialog

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.fi开发者_开发问答ndElement(By.name("Passport")).click();

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


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();
}


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();
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜