Home > Mobile >  Not able to click the "Choose File" button in selenium webdriver. The upload window for fi
Not able to click the "Choose File" button in selenium webdriver. The upload window for fi

Time:11-03

I was suppose to click a " Choose File " button in a website and it is supposed to open a window that allow me to choose the file but the window doesnot open evenn 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 window should open but its not.

CodePudding user response:

<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

CodePudding user response:

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();
  • Related