我在网页上遇到一个元素问题。当我想以某种方式与它进行交互(点击它,发送钥匙或其他东西)并且每次我都收到错误IDE view。在浏览器中,我可以单击此按钮,然后找到它browser view。本网站上的所有其他元素都运作良好。我的任务是上传文件。在这种情况下,我想做这个 - > filePathUploadButton.sendKeys("C:\\Users\\ASuvorkov\\Desktop\\testverlG_serenity.png")
。
手动就是这样:
您对此有任何建议或经验,因为我还没有遇到过这样的事情吗?
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)
答案 0 :(得分:1)
我们无法使用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);