1

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()
2
  • Why don't you just use .sendKeys against the file upload control? Commented Oct 5, 2013 at 11:19
  • Can you explain pls ? Commented Oct 5, 2013 at 11:31

2 Answers 2

1

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()
Sign up to request clarification or add additional context in comments.

5 Comments

Hi,I try but don't work do you have email ? to send you the link of website
Umm yes of course. But no. sorry I'm not your programming tutor. Edit your question to contain the information about what you have tried, and how it is failing.
Tks for your help . i try with your code but python tell me no element document .i don't understand document.querySelector ? tks
The last line is javascript. You'll need to run that via driver.execute_script
Actually I had an error in my javascript. There was a third argument that got in there due to a copy paste error. setAttribute should only have two arguments.
1

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.

Comments

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.