元素不可用 - 硒
Element not available - Selenium
我对网页上的一个元素有疑问。当我想以某种方式与它交互时(单击它、sendKeys 或其他东西)并且每次我收到错误 IDE view. In the browser I can click on this button and I locate it right browser view。该网站上的所有其他元素都运行良好。我的任务是上传文件。在这种情况下,我想做这个 -> filePathUploadButton.sendKeys("C:\Users\ASuvorkov\Desktop\testverlG_serenity.png")
。
手动会是这样:
- 用户点击了这个按钮
- 然后用户选择 dialog
中的文件
- 然后用户点击 "open file" 上传文件
你有什么建议或经验吗,因为我还没有遇到过这样的事情?
PS 即使检查 element.isEnabled()
、element.isDisplayed()
是否会导致此错误和程序中断。
代码片段(JAVA):
@FindBy(id = "fileupload") private WebElement filePathUploadButton;
public void downloadCoverPicture() {
waitABit(LONG_WAIT);
element(mediaTypeDropDownButton).waitUntilClickable();
mediaTypeDropDownButton.click();
element(mediaTypeDropDownList).waitUntilClickable();
mediaTypeDropDownList.click();
waitABit(LONG_WAIT);
element(filePathUploadButton).waitUntilClickable();
filePathUploadButton.sendKeys("C:\Users\ASuvorkov\Desktop\testverlG_serenity.png");
waitABit(50000);
}
错误堆栈跟踪:
org.openqa.selenium.ElementNotVisibleException: The following error occurred: Timed out after 10 seconds. Element not available
at pages.NewBookAddPage.downloadCoverPicture(NewBookAddPage.java:71)
at steps.NewBookManagementSteps.downloadsCoverPicture(NewBookManagementSteps.java:52)
at tests.NewBookManagementTest.addsNewBook(NewBookManagementTest.java:43)
我们无法使用 selenium
与打开的文件对话框交互。我们可以使用 robot class
或 autoit
作为用途。
点击文件上传按钮,使用下面的robot class
文件上传实现代码。
Robot robot = new Robot();
StringSelection selection = new StringSelection("Absolute path of the file");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
我对网页上的一个元素有疑问。当我想以某种方式与它交互时(单击它、sendKeys 或其他东西)并且每次我收到错误 IDE view. In the browser I can click on this button and I locate it right browser view。该网站上的所有其他元素都运行良好。我的任务是上传文件。在这种情况下,我想做这个 -> filePathUploadButton.sendKeys("C:\Users\ASuvorkov\Desktop\testverlG_serenity.png")
。
手动会是这样:
- 用户点击了这个按钮
- 然后用户选择 dialog 中的文件
- 然后用户点击 "open file" 上传文件
你有什么建议或经验吗,因为我还没有遇到过这样的事情?
PS 即使检查 element.isEnabled()
、element.isDisplayed()
是否会导致此错误和程序中断。
代码片段(JAVA):
@FindBy(id = "fileupload") private WebElement filePathUploadButton;
public void downloadCoverPicture() {
waitABit(LONG_WAIT);
element(mediaTypeDropDownButton).waitUntilClickable();
mediaTypeDropDownButton.click();
element(mediaTypeDropDownList).waitUntilClickable();
mediaTypeDropDownList.click();
waitABit(LONG_WAIT);
element(filePathUploadButton).waitUntilClickable();
filePathUploadButton.sendKeys("C:\Users\ASuvorkov\Desktop\testverlG_serenity.png");
waitABit(50000);
}
错误堆栈跟踪:
org.openqa.selenium.ElementNotVisibleException: The following error occurred: Timed out after 10 seconds. Element not available
at pages.NewBookAddPage.downloadCoverPicture(NewBookAddPage.java:71)
at steps.NewBookManagementSteps.downloadsCoverPicture(NewBookManagementSteps.java:52)
at tests.NewBookManagementTest.addsNewBook(NewBookManagementTest.java:43)
我们无法使用 selenium
与打开的文件对话框交互。我们可以使用 robot class
或 autoit
作为用途。
点击文件上传按钮,使用下面的robot class
文件上传实现代码。
Robot robot = new Robot();
StringSelection selection = new StringSelection("Absolute path of the file");
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(selection,null);
robot.keyPress(KeyEvent.VK_CONTROL);
robot.keyPress(KeyEvent.VK_V);
robot.keyRelease(KeyEvent.VK_CONTROL);
robot.keyRelease(KeyEvent.VK_V);
robot.setAutoDelay(2000);
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);