【问题标题】:Not able to click the "Choose File" button in selenium webdriver. The upload window for file is not opening无法单击 selenium webdriver 中的“选择文件”按钮。文件的上传窗口未打开
【发布时间】:2021-11-03 07:29:01
【问题描述】:

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

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

上传后,窗口应该会打开,但实际上并没有。

【问题讨论】:

  • 文件上传和下载对话框属于操作系统而不是浏览器。这就是 selenium 无法访问的原因。

标签: java selenium selenium-webdriver


【解决方案1】:
<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");

输出

【讨论】:

    【解决方案2】:

    尝试操作,如下所示:

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

    【讨论】:

    • 不会工作 bcs 文件上传和下载对话框属于操作系统而不是浏览器。这就是 selenium 无法访问的原因。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-02
    • 1970-01-01
    相关资源
    最近更新 更多