7

I'm trying to upload a png via selenium. My Problem is, that the Input I need to use, is invisible to selenium, but not to the user. In the FAQ of Selenium they told me to use the JavascriptExcecutor like:

((JavascriptExecutor)driver).executeScript("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", fileUploadElement);

I used this with C# in the past, and it worked, but now im struggeling to convert that usage to python. I would use the document.getElementByName() function, but the input doesn't have a Name and there are more than one on the page. What is the best way to solve that Problem. I already tried

icon = element.find_element_by_css_selector("input")
script_befehl = icon+".style.visibility = 'visible'; "+icon+".style.height = '1px'; "+icon+".style.width = '1px'; "+icon+".style.opacity = 1

but that also didn't work, i'm getting a Syntax error

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Chorgo
  • 71
  • 1
  • 6
  • Invisible to Selenium but not to the user? How does that work? Sounds like there is an extra step to "make it visible" that you aren't doing. Are you sure it's invisible? If you interact with it, what does Selenium do? – Arran Sep 08 '14 at 14:39

2 Answers2

6

There is an execute_script() method on the driver instance, arguments are passed to it in a similar to C#'s JavascriptExecutor:

icon = element.find_element_by_css_selector("input")
driver.execute_script("arguments[0].style.visibility = 'visible'; arguments[0].style.height = '1px'; arguments[0].style.width = '1px'; arguments[0].style.opacity = 1", icon)
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thats easier than expected, but after the script, the webelement is still invisible, is there another way to upload my file? – Chorgo Sep 08 '14 at 13:52
  • @user3652291 hm, let's see, can you try the "actionchains" approach, for starters. See: http://stackoverflow.com/a/7340952/771848. Let me know if it helped or not. Thanks. – alecxe Sep 08 '14 at 13:53
  • I tried it like this ActionChains(driver).move_to_element(icon).send_keys_to_element(icon,largepic).perform() but Im getting the error: "Offset within element cannot be scrolled into view: (72, 12.5)" – Chorgo Sep 08 '14 at 14:39
  • @user3652291 hm, can you share the link to the website and the code you have? Otherwise I'm shooting in the dark. Thank you. – alecxe Sep 08 '14 at 14:53
0

In my case, the invisibility of the element was due to having display:none; in the style. So the solution was:

driver.execute_script("arguments[0].style.display = 'block';", element)
Pablo Guerrero
  • 936
  • 1
  • 12
  • 22