1

I am using selenium webdriver to try and emulate file uploads

I have a file input field:

<input class="upload_file_field" id="main_image_file" label="false" name="banner_image_file" type="file" />

Using this in my test results in a ElementNotVisibleError

@driver.find_element(:id, "main_image_file").send_keys "/Users/dan/Desktop/sample.jpg"

I even tried to change the visibility before trying to upload the file:

@driver.execute_script("arguments[0].style.visibility='visible'; arguments[0].style.display='block'; arguments[0].style.height='100px'; arguments[0].style.width='100px'; arguments[0].style.opacity=1; arguments[0].style.zindex=20", @driver.find_element(:id, "main_image_file"))

It still results in an ElementNotVisibleError

Any advice would be much appreciated

UPDATE

Okay, turns out that there are several conditions which can make an element hidden:

  1. Cannot have 0 opacity
  2. Must have length and width > 0
  3. Cannot be hidden visibility
  4. Display can't be none
  5. transform property can move elements off the page which make it not visible

My problem was with the transform property. it was a way of hiding the input field by transforming it off the page completely so I could use a nicer style button using jQuery file-upload.

After that,

@driver.find_element(:id, "main_image_file").send_keys "/Users/dan/Desktop/sample.jpg"

worked perfectly well in emulating user selection of a file

4

2 回答 2

0

可能只是由于某些错误而导致页面未加载的情况。您能否尝试出错:

raise(@driver.page_source.to_s)

就在 find_element 命令之前?

通过这种方式,您可以确保输入字段实际上在页面上,如 Selenium 所见。

于 2013-05-27T21:29:38.210 回答
0

您使用 Ruby,但无论如何,如果这个问题对您来说仍然是实际的,那么请查看我提供答案的类似问题How to click on <input type=file> across browsers using Selenium Webdriver?

于 2013-09-09T06:38:09.177 回答