【问题标题】:How to upload file without clicking on the upload button( upload button not starting with <input> tag) using Selenium Webdriver.?如何使用 Selenium Webdriver 在不点击上传按钮(上传按钮不以 <input> 标签开头)的情况下上传文件?
【发布时间】:2015-06-18 18:11:04
【问题描述】:

在运行竹子(CI)时,我的脚本失败,所有“上传文件链接”都不是以输入标签开头的。我正在使用 Auto IT 上传在本地运行良好的文件,当我尝试在远程机器上运行 Bamboo 时,它在那里失败了。

所以想从我不想点击上传按钮的后端上传文件。

Sendkeys() 尝试过,但在这种情况下没有帮助。

提前问好

【问题讨论】:

    标签: selenium-webdriver bamboo


    【解决方案1】:

    你为什么不能试试这个。你的网页是否有定位文件的浏览按钮?如果是,则将元素传递给 locatorUpload para 并将上传按钮元素传递给 locatorButton

    public void UploadFile(By locatorUpload, By locatorButton, String filePath){
    
            driver.findElement(locatorUpload).sendKeys(filePath);
            waitForElementClickable(driver, locatorButton, 4);
            driver.findElement(locatorButton).click();
    
        }
    
    public void waitForElementClickable(WebDriver driver, By locator, Integer timeoutInSeconds){
        WebDriverWait wait = new WebDriverWait(driver, timeoutInSeconds);
        wait.until(ExpectedConditions.elementToBeClickable(locator));
    }
    

    【讨论】:

      【解决方案2】:

      好吧,通常在这种情况下你需要做 3 个步骤:

      • 使用 javascript 使 input[type=file] 元素可见
      • 找到此输入并使用 send_keys(PATH_TO_FILE_ON_YOUR_MACHINE)
      • 使用javascript触发上传

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-10-15
        • 1970-01-01
        • 2015-11-09
        • 2013-11-28
        • 2022-12-20
        • 1970-01-01
        相关资源
        最近更新 更多