【问题标题】:WebDriver Selenium browse file JavaWebDriver Selenium 浏览文件 Java
【发布时间】:2015-06-17 07:37:45
【问题描述】:

无法使用 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 code

【问题讨论】:

  • NoSuchElementException 并非来自您的代码 sn-p。因此,即使在等待之前,您也在代码中某处寻找 id 为 BatchUploadPlugin_57_fileupload 的元素。我相信您甚至在 DOM 加载之前就在寻找该元素。用完整的代码 sn-p 更新问题。

标签: java eclipse junit webdriver


【解决方案1】:

从您提供的屏幕截图中,我可以看到 相关元素位于 iframe 中。

(查看开发者工具栏下方的第二个栏,其中包含:Inspector、Console 等。您会注意到 iframe#iframe_1434526152814_57_7。)。

因此,您无法将路径发送到它。

为了发送上传路径,需要先切换到frame,然后发送路径到“Browse”元素上传。

对于切换框架,您可以使用下面的代码(我可以从屏幕截图中看到的框架ID是iframe_1434526152814_57_7,我可以放心地假定它是动态的并且不能用作要切换到的框架的 ID。所以,我假设页面中只有 1 个框架,因此代码。):

driver.switchTo().frame(0);

然后,使用以下代码发送元素的路径

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

【讨论】:

  • 有效!!非常感谢!
  • 很高兴它为您解决了。请也接受答案。它将帮助其他面临类似问题的人,直接检查出来。 :)
【解决方案2】:

请按以下方式进行:

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

.File 元素不应出现在 id 中。

【讨论】:

  • 对不起,我不是在测试,因为我不知道那里有什么问题。那么您可能知道它可能是什么,为什么它不起作用?
  • 否(有一个错误 - “NoSuchElementException:无法定位元素:...”
  • 我明白了,请从文件浏览代码上方的代码中删除隐式等待。并添加thread.sleep(10000)
  • 做了,但有同样的错误,没有帮助。我什至不知道它是什么
  • 我知道为什么它不起作用,这是因为我的按钮在不可见元素中:prntscr.com/7idgkg 我想是因为这个,但是如何让它可见?
【解决方案3】:

我不是硒方面的专家,但看起来您发布的代码的 sn-p 无法引发此异常。当它搜索 BatchUploadPlugin_57_fileupload 时,它是表单 ID。您的代码正在搜索一个输入,其 id 为 1434461513889_57_7_input

我还发现driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS); 可能无法像these 开发人员所期望的那样工作。尝试将其替换为Thread.sleep(3000);。我知道不建议对 selenium 使用线程睡眠,而只是为了测试。

【讨论】:

  • 换成“Thread.sleep(3000)”后出现错误“NoSuchElementException: Unnable to locate element: ...”
【解决方案4】:

试试下面的代码.. 你需要通过上传文本框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));
}

【讨论】:

  • locatorUpload 和 locatorButton 有什么区别?我在表单上只有一个按钮。并且在“waitForElementClickable”上有一个错误 - 未定义的方法
  • 我也更新了 waitForElementClickable() 函数。您的网页是否有定位文件的浏览按钮?如果是,则将元素传递给 locatorUpload para 并将上传按钮元素传递给 locatorButton
  • 我明白了,谢谢。但是我应该分配给 locatorUpload 和 locatorButton 什么?我有按钮的 ID,它是“1434461513889_57_7_input”,但我不知道 locatorUpload 是什么? prntscr.com/7hx49e
【解决方案5】:

对于处理原生元素,你为什么不能试试 Sikuli 与 Selenium 脚本的集成。 您可以参考此链接了解更多详情。 http://selenium-suresh.blogspot.in/2014/01/sikuli-automation-tool-integration-with.html

【讨论】:

    猜你喜欢
    • 2012-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-10
    • 2016-05-26
    • 2021-07-12
    • 2023-03-21
    相关资源
    最近更新 更多