-1

I have a problem with one element on the web page. When I want somehow to interact with it(click on it, sendKeys or something) and every time I'm getting an error IDE view. In the browser I can click on this button and I locate it right browser view. All other elements on this website are working well. My task is to upload a file. In this case i want to make this -> filePathUploadButton.sendKeys("C:\\Users\\ASuvorkov\\Desktop\\testverlG_serenity.png") .

Manually it would be so:

  1. User clicks on this button
  2. Then user selects file in dialog
  3. And the user clicks on "open file" to upload a file

Do you have any suggestions or experience with it, because I didn't meet such kind of things yet?

PS even checking if element.isEnabled(), element.isDisplayed() causes this error and programm breaks up.

Code snippet(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);
}

Error stacktrace:

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)
Andrei Suvorkov
  • 5,559
  • 5
  • 22
  • 48

1 Answers1

1

We cannot use selenium to interact with open file dialog. We can use robot class or autoit for that uses.

Click the file upload button and use the below robot class file upload implementation code.

    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); 
Pradeep hebbar
  • 2,147
  • 1
  • 9
  • 14
  • Thank you for your suggestion, I will try it on Tuesday and write you if it works for me. – Andrei Suvorkov Jan 13 '18 at 17:15
  • Thank you very much! It works fine for me. Also the problem was with the selector `@FindBy(id = "fileupload") private WebElement filePathUploadButton;`. I do not know why, but this particular selector didn't work. I have tried several possibilities and found that `@FindBy(xpath = "//span[contains(.,'Durchsuchen...')]") private WebElement filePathUploadButton;` don't throw any exception. Your `Robot` class works fine. – Andrei Suvorkov Jan 16 '18 at 08:30