1

I've tried

File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");
driver.findElement(By.xpath("//input[@class='libray_create-resource_choose-file_hidden-input']")).sendKeys(file.getAbsolutePath());

And My HTML is something like below

 <div class="libray_create-resource_choose-file">
      <button class="btn is-hollow_blue libray_create-resource_choose-file_button undefined">Choose File</button>
      <input type="file" class="libray_create-resource_choose-file_hidden-input">
   </div>
   
   <div class="library_create-modal_footer">
      <button class="btn is-text_only btn-cancel undefined">Cancel</button>
     <button class="btn is-filled_blue undefined" disabled="">Add</button>
</div>

I found that file input which is hidden got the file path set properly.

The problem is there Choose File button element is different from file input element //input[@class='libray_create-resource_choose-file_hidden-input']"

There seems to some JS event which make final Add button enable on click of Choose File button.

So, I imported file into file HTML element but how can I enable Add button?

I tried to make that button enabled

WebElement yourButton= driver.findElement(By.className("is-filled_blue"));

JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].removeAttribute('disabled','disabled')",yourButton);

WebDriverWait wait = new WebDriverWait(driver, 20);
wait.until(ExpectedConditions.elementToBeClickable(yourButton));

It makes that button visible but still not allow to actually click on it.

8
  • There is not submit button in your question Commented Apr 10, 2021 at 16:56
  • Try to press enter after entering file name: driver.findElement(By.xpath("//input[contains(@id,'html5') and @type='file']")).sendKeys(Keys.ENTER); Commented Apr 10, 2021 at 16:56
  • Can you please provide the link to the page you are working with? Commented Apr 10, 2021 at 21:11
  • Apologies, the business URL I can't share here. It even need login too. :( Commented Apr 11, 2021 at 2:05
  • When you paste manually the path , does it enable the submit button Commented Apr 11, 2021 at 4:34

5 Answers 5

2
+25

First let me say that -- I don't program in Java, -- but I do a lot of selenium coding in Python.

When I was looking at this question from purely a selenium view point I noted that you aren't selecting the choose file button prior to sending your input. I would assume that you have to select this button to enable the element - btn is-filled_blue undefined

Here is some pseudocode that might help trigger that button to switch from disabled to enabled.

File file = new File("/Users/swapnil.kotwal/Desktop/AntVsGradle.jpg");

WebElement select_button = driver.findElement(By.xpath("//*[@class='btn is-hollow_blue libray_create-resource_choose-file_button undefined']"));

select_button.click();

WebElement upload_file = driver.findElement(By.xpath("//input[@class='libray_create-resource_choose-file_hidden-input']"));

upload_file.sendKeys(file.getAbsolutePath());

/* You might need to send an Enter, Return or Tab before the 
   add_file_button changes.  This process requires testing on the website. 

   upload_file.sendKeys(Keys.ENTER); 
   upload_file.sendKeys(Keys.RETURN); 
   upload_file.sendKeys(Keys.TAB); 
*/

boolean add_file_button_presence = driver.findElement(By.className("is-filled_blue")).isDisplayed();
boolean add_file_button_enabled = driver.findElement(By.className("is-filled_blue")).isEnabled();

if (add_file_button_presence==true && add_file_button_enabled==true)
    {
       WebElement add_file_button = driver.findElement(By.className("is-filled_blue"));
       add_file_button.click();
    }


Since I don't normally program in Java the structure and syntax of my pseudocode could be incorrect. If it is please let me know and I will either delete this answer or correct the issues with some research.

Sign up to request clarification or add additional context in comments.

9 Comments

Unfortunately. By any way that Windows File Upload pop-up not going away ... tried below options. 1. select_button.click(); select_button.submit(); and 2. action.sendKeys(Keys.ESCAPE).perform(); also 3. select_button.sendKeys(Keys.ESCAPE); & 4. robot = new Robot(); obot.keyPress(KeyEvent.VK_ESCAPE); robot.keyRelease(KeyEvent.VK_ESCAPE); none of this hiding that pop-up on Mac OS
I'm sorry that it didn't work for your use case. I would have posted a second method if you would have gotten back to me with these issues. I still believe that the code above is useful when combined with JavascriptExecutor correctly.
Thank you so much Sir.. You are so much supportive. I'll definitely find out some way.. after all each system come up with its own beauty and challenges 🙏🏻
It's hard to troubleshoot this problem remotely. I'm interested in what happens to the "is-filled_blue" button when you do the add file process manually? What Attribute does it get? Enabled?
Yes, it enables only when that pop-up get satisfied. I've managed to inject the driver.findElement(By.xpath("//input")).sendKeys(file.getAbsolutePath()); but still that button won't enable. I'll post my final answer and other findings here. don't worry
|
1

That undefined class looks suspect - try removing it by adding another js call: js.executeScript("arguments[0].style.removeProperty('undefined')",yourButton);

Comments

1

My suggestion would be always limit the usage of JavaScript(JavascriptExecutor) code to an extent it simulates the end user action and not manipulates the application behavior like enabling a button which is disabled functionally etc.

Our purpose is to simulate the application steps in the same way how an end user would use it.

I would suggest to use any third part tool like AutoIt/Sikuli to handle this case since sending file path doesn't enable the 'Add' button automatically as expected

1)Click on the 'Choose File' button using Selenium which opens the upload window, which is not a web component.

2)Since Upload window is not a web component Selenium doesn't support it.

3)Use any third party tools like AutoIt/Sikuli to handle the Windows upload popup by setting the filepath and submit.

4)As we are uploading the file in the UI in the same way how an end user would do, 'Add' button will be enabled automatically.

AutoIt

Comments

1

Try to use JavascriptExecutor to click it too, before you remove disabled attribute.

WebElement element = driver.findElement(By.xpath("xpath"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].click();", element);

If you use JavascriptExecutor you dont need to make it visible

Comments

1

I managed to upload file by using below hack... I never click on Choose File link/button which was launching Upload File Windows pop-up and by any mean that windows pop-up was not going away.

Tried below options.

1.

select_button.click(); 
select_button.submit(); 
action.sendKeys(Keys.ESCAPE).perform(); 
select_button.sendKeys(Keys.ESCAPE);
robot = new Robot(); 
robot.keyPress(KeyEvent.VK_ESCAPE); 
robot.keyRelease(KeyEvent.VK_ESCAPE);

none of this hiding that pop-up on Mac OS

So, I found a Jugad(#hack in English) I never click on Choose File button which were launching that Upload File Window

public void uploadFile() {
    WebElement element = driver.findElement(By.className("libray_create-resource_choose-file_hidden-input"));
    element.sendKeys("/Users/swapnil.kotwal/projectName/automation.pdf");
    WebElement addButton = driver.findElement(By.className("is-filled_blue"));
    // click was important, thanks to this answer in this thread   https://stackoverflow.com/a/67095019/1665592
    driver.executeScript("arguments[0].click();", addButton); 
    driver.executeScript("arguments[0].removeAttribute('disabled','disabled'); arguments[0].style = \"\"; arguments[0].style.display = \"block\"; " +
     "arguments[0].style.visibility = \"visible\";", addButton);
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.elementToBeClickable(addButton));
    addButton.click();
}

1 Comment

This answer deserve an upvote. Congrats on solving this on your end. Some problems are just hard to solve remotely. Happy Coding!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.