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)
-
Similar for JS: Set value of input instead of sendKeys() - selenium webdriver nodejs at SUkenorb– kenorb2015年05月23日 19:56:58 +00:00Commented May 23, 2015 at 19:56
1 Answer 1
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.
-
4You 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– Sam Woods2012年07月03日 22:36:11 +00:00Commented Jul 3, 2012 at 22:36