user2849267
user2849267

Reputation: 33

Upload image python selenium javascript

i need your help to upload image with selenium python I have a form

                            <div class="none upload_no_autosubmit" id="upload_image_base_pack">
                            <div class="labelform inputfile">Choose</div>
                            <div class="lbcinputfile">
                            <input type="file" name="image0" id="image0" class="input_file">
                            </div> 
                            <div class="clear"></div>
                            <div class="lbcinputfile_submit">
                            <input type="submit" class="button-upload" value="add" onclick="return disabled_onsubmit_photosupCheck('#image0', 'upload_image_base_pack');">
                            </div>
                            <div class="clear"></div>
                            </div>
                            <div class="message info right no_autosubmit" id="message_upload_image_base_pack">

I use selenium with python and i want upload image file with this

driver.execute_script("return disabled_onsubmit_photosupCheck('c:/1bo.jpg', 'upload_image_base_pack')")

pls help tks


don't work

driver.execute_script("document.querySelector('#image0').setAttribute('value', 'c:/1bo.jpg', 'upload_image_base_pack')")
input_element = driver.find_element_by_css_selector("input[name='image0']")
input_element.send_keys("c:/1bo.jpg")
driver.find_element_by_css_selector("input[type='submit']").click()

Upvotes: 1

Views: 2352

Answers (2)

Norbert.S
Norbert.S

Reputation: 21

Try something like:

def test_TC1(self):
        driver = self.driver
        driver.find_element_by_xpath("//div/form/table/tbody/tr[2]/td[2]/input").clear()
        driver.find_element_by_xpath("//div/form/table/tbody/tr[2]/td[2]/input").send_keys("C:\\FILE.xml")

From my experience you don't need click, but only send_keys.

Upvotes: 1

aychedee
aychedee

Reputation: 25569

Use the webdriver methods to find the correct input element, enter the file name, and click the submit button. Like this:

input_element = driver.find_element_by_css_selector("input[name='image0']")
input_element.send_keys("c:/1bo.jpg")
driver.find_element_by_css_selector("input[type='submit']").click()

However: Because that site uses the file chooser and you can't interact with that dialog via Selenium (as far as I know) you will have to set the value of the input with javascript. So something like this should work:

driver.execute_script('document.querySelector("#image0").setAttribute("value", "c:/1bo.jpg")')
driver.find_element_by_css_selector("input[type='submit']").click()

Upvotes: 1

Related Questions