Snk2611uk
Snk2611uk

Reputation: 11

Not able to click the "Choose File" button in selenium webdriver. The upload window for file is not opening

I was supposed to click a " Choose File " button on a website and it is supposed to open a window that allows me to choose the file but the window does not open even if I code it to click the element.

System.setProperty("webdriver.chrome.driver","C:\Users\shash\eclipse-wo"); 

WebDriver driver=new ChromeDriver();

driver.navigate().to("http://the-internet.herokuapp.com/upload"); 

Thread.sleep(5000);
WebElement uploadPhotoBtn = driver.findElement(By.xpath("//input[@id='file-upload']")) 

uploadPhotoBtn.click();

after this upload, the window should open but it's not.

Upvotes: 1

Views: 3407

Answers (2)

pmadhu
pmadhu

Reputation: 3433

Try with Actions, like below:

Imports Required 
import org.openqa.selenium.interactions.Actions;

driver.get("http://the-internet.herokuapp.com/upload");
        
Actions actions = new Actions(driver);
        
WebElement uploadPhotoBtn = driver.findElement(By.xpath("//input[@id='file-upload']"));
actions.moveToElement(uploadPhotoBtn).click().perform();

Upvotes: -1

Nandan A
Nandan A

Reputation: 2922

<input id="file-upload" type="file" name="file">

The HTML tag type is input instead of click you can directly send the file location to the element.

Code:

    driver = new ChromeDriver();
    driver.navigate().to("http://the-internet.herokuapp.com/upload");
    Thread.sleep(5000);
    WebElement uploadPhotoBtn = driver.findElement(By.xpath("//input[@id='file-upload']"));
    uploadPhotoBtn.sendKeys("C:\\Sample.json");

Output:

enter image description here

Upvotes: 3

Related Questions