簡體   English   中英

WebDriver Selenium瀏覽文件Java

[英]WebDriver Selenium browse file Java

無法使用webdriver瀏覽文件。

driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.findElement(By.id("1434461513889_57_7_input.file")).sendKeys("C:\\PDF_V1_COL88810_6L_Frangipani_TL_Fr_P1211089.pdf");

有這個錯誤:

NoSuchElementException:無法找到元素:{“ method”:“ id”,“ selector”:“ BatchUploadPlugin_57_fileupload”}

HTML代碼

請按以下方式進行:

driver.findElement(By.id("1434461513889_57_7_input")).sendKeys("C:\\PDF_V1_COL88810_6L_Frangipani_TL_Fr_P1211089.pdf");

.File元素不應在ID中存在。

我不是硒專家,但是您發布的代碼片段似乎無法引發此異常。 在搜索BatchUploadPlugin_57_fileupload ,它是表單ID。 您的代碼正在搜索輸入,其ID為1434461513889_57_7_input

我還發現的是driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 對於這些開發人員可能無法正常工作。 嘗試將其替換為Thread.sleep(3000); 我知道硒不建議使用線程睡眠,而只是為了測試。

嘗試下面的代碼..您需要傳遞上載文本框webelement和上載按鈕webelement。

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

從您提供的屏幕截圖中,我可以看到相關元素在iframe中。

因此,您無法將路徑發送給它。

為了發送上傳路徑, 您需要先切換到框架,然后將路徑發送到元素“瀏覽”進行上傳。

: 對於切換框架 ,您可以使用以下代碼

driver.switchTo().frame(0);

然后,使用以下代碼將路徑發送到元素

driver.findElement(By.xpath("//input[@name='userfile']")).sendKeys("C:\\PDF_V1_COL88810_6L_Frangipani_TL_Fr_P1211089.pdf");

對於處理本機元素,為什么不能嘗試與Selenium腳本進行Sikuli集成。 您可以參考此鏈接以獲取更多詳細信息。 http://selenium-suresh.blogspot.in/2014/01/sikuli-automation-tool-integration-with.html

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM