cant click button which opens file attachment dialog

荒凉一梦 提交于 2019-12-12 16:36:54

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/4667048/cant-click-button-which-opens-file-attachment-dialog

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!