Click the button that opens the file attachment dialog

I am using selenium 2 beta . I try to click a button that opens a file attachment dialog. but when I click, nothing happens.

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


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

using only selenium, not selenium 2, I can easily click it.

+3
source share
2 answers

I assume that the problem is only related to using Internet Explorer, since IE and FF handle file input in a slightly different way: in FF you can click on a button or field to open the Open dialog box, while in IE you can click on or double-click on the field.

WebDriver , , .

Selenium 1, JavaScript . WebDriver, JavaScript:

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

abouve Firefox, - :

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

:

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

Source: https://habr.com/ru/post/1784912/


All Articles