无法单击 selenium webdriver 中的“选择文件”按钮。文件上传窗口未打开

问题描述 投票:0回答:3

我应该单击网站上的“选择文件”按钮,它应该打开一个窗口,允许我选择文件,但即使我对其进行编码以单击该元素,该窗口也不会打开。

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

上传后,窗口应该会打开,但事实并非如此。

java selenium selenium-webdriver
3个回答
3
投票
<input id="file-upload" type="file" name="file">

HTML 标签类型为

input
,无需点击即可直接将文件位置发送至
element

代码

    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");

输出

enter image description here


0
投票

非常感谢。 actions.moveToElement(uploadPhotoBtn).click().perform();工作了


-1
投票

尝试使用Actions,如下所示:

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();
© www.soinside.com 2019 - 2024. All rights reserved.