代码之家  ›  专栏  ›  技术社区  ›  sandhya sasane

用JAVA处理打开的文件对话框

  •  -1
  • sandhya sasane  · 技术社区  · 8 年前

    select file dialog box 我无法通过Selenium选择文件。

    ChromeOptions options = new ChromeOptions();
    options.addArguments("--disable-notifications");
    
    WebDriver driver = new ChromeDriver(options);
    driver.get("https://xyz.abc.pqr");
    
    try 
    {
        WebDriverWait wait = new WebDriverWait(driver, 60);
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("element_id_1")));
    
        System.out.println("Loaded and in....");
    
        WebElement AttachMenu = driver.findElement(By.className("Class_1"));
        AttachMenu.click();
        System.out.println("Clicked Attach menu....");
    
        // <input type="file" accept="image/*,video/*" multiple="" style="display: none;">
    
        // As you can see above input element is not having any id or class
    
        // How do i sendkeys to above input element that has opened dialog
    
        // this is the question...
    
    }
    catch (TimeoutException e)
    {
        System.out.println("Element not found...");
    }
    
    // driver.quit();
    

    它不是上传,它只是选择文件;我会点击上传按钮之后…

    1 回复  |  直到 8 年前
        1
  •  0
  •   sandhya sasane    8 年前

    1. <div role="button" title="Attach">
    2. 如何 sendkeys <input type="file" accept="image/*,video/*" multiple="" style="display: none;">

    driver.findElement(By.xpath("//div[@title='Attach']")).click();
    System.out.println("Attach clicked....");
    
    WebElement MediaButton = driver.findElement(By.className("xyzmm"));
    MediaButton.click();
    
    System.out.println("Select clicked....");
    WebElement uploadElement = driver.findElement(By.xpath("//input[@accept='image/*,video/*']"));
    
    uploadElement.sendKeys("/Users/Apple/Desktop/logos/Team1.png");
    

    静止 uploadElement display none 有问题…,正在尝试…这个问题已经解决了