user6645934
user6645934

Reputation: 1

Upload file with hidden input with Selenium WebDriver Python

Html:

<div id="js-cert-file" class="form-group">
<button id="js-ob-browse-n-upload" class="btn btn-ob browse-and-upload-onboarding-ssl-button" style=""> BROWSE & UPLOAD </button>
<input id="js-cert-file" class="hidden btn btn-ob" type="file" accept=".p12, .pem, .pfx" name="file">
<input id="file-name" type="text" disabled="" value="File Name" style="display:none">
</div>

I have tried uploading the document using the xpath and css selector but not able to do it since the input is hidden. I have spend few days banging my head on this and still not able to figure it out so thought it was time to ask the experts, please help!

Issue is that, I want to upload the file without clicking the "Browse and Upload" Button, but like I said am not able to do it since the input is hidden.

Here the my python code:

BrowseAndUpload = driver.find_element_by_xpath("/html/body/div[3]/div/div[2]/div/div/div[1]/div[1]/input[1]")
clickBrowseAndUpload.send_keys('file full path')

Upvotes: 0

Views: 1825

Answers (2)

Norayr Sargsyan
Norayr Sargsyan

Reputation: 1868

You should change the visibility of the input field and also you should change the type attribute to file

JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.getElementById('your_input_field_id').style.visibility='visible'");
executor.executeScript("document.getElementById('your_input_field_id').setAttribute('type', 'file')");

WebElement input = driver.findElement(By.id("your_input_field_id"));

input.sendKeys("you\\pat\\to\\Uploadfile.fileExtension");

Upvotes: 0

Andersson
Andersson

Reputation: 52665

Try to make input field visible and upload file with following code:

driver.execute_script('document.getElementById("js-cert-file").style.visibility="visible";')
driver.execute_script('document.getElementById("js-cert-file").style.display="block";')
driver.find_element_by_xpath('//input[@id="js-cert-file"]').send_keys('file full path')

Upvotes: 1

Related Questions