10

I am trying to automate uploading an image. The uploader is a Flash object and using AutoIt is not an option. So I am trying to work my way around this with just webdriver.

The idea is to 'set' the value attribute of the image uploader with a link pointing to an image saved on the server. My question is, is there a method to set the attribute of an element in webdriver? I know there is one to get value (get_attribute). I am using Python/Selenium.

The input field is:

input type="hidden" value="" name="allImages" id="allImages"

and I have tried:

image = wd.find_element_by_id("allImages")
image.value = "http://optimusprime/uploads/b31f8a31-9d4e-49a6-b613-fb902de6a823.jpg"

The script runs without error, but the attribute value is not set. Can anybody please point me to the right way of setting attributes? (if there is one)

12

I wrote a javascript snippet as follows after reading replies from the webdriver google group:

wd.execute_script("document.getElementById('allImages').value = '../uploads/b31f8a31-9d4e-49a6-b613-fb902de6a823.jpg';")

Or as Sam suggested in the comment:

image = wd.find_element_by_id("allImages")
wd.execute_script("arguments[0].value = 'foo.jpg';", image) 

Answer from the webdriver google group:

Using the "execute_script" method is the recommended approach in this case.

No, none in WebDriver, since it aims to imitate a user and a user cannot set attributes directly.

  • 4
    You can simplify this with this code: image = wd.find_element_by_id("allImages") wd.execute_script(arguments[0].value = '../uploads/b31f8a31-9d4e-49a6-b613-fb902de6a823.jpg';", image) – Sam Woods Jul 3 '12 at 22:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.