12

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)

alecxe
11.4k11 gold badges52 silver badges107 bronze badges
asked Jul 3, 2012 at 19:24
1

1 Answer 1

14

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.

answered Jul 3, 2012 at 22:20
1
  • 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) Commented Jul 3, 2012 at 22:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.