Swapnil Kotwal
Swapnil Kotwal

Reputation: 5730

Selenium Upload File On file selection doesn't enable submit button

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.

Upvotes: 1

Views: 2068

Answers (5)

Swapnil Kotwal
Swapnil Kotwal

Reputation: 5730

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

Upvotes: 1

Raclos
Raclos

Reputation: 85

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

Upvotes: 1

Nischitha K N
Nischitha K N

Reputation: 407

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

Upvotes: 1

SystemsInCode
SystemsInCode

Reputation: 699

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

Upvotes: 1

Life is complex
Life is complex

Reputation: 15619

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.

Upvotes: 2

Related Questions