简体   繁体   中英

Ruby/Selenium/Watir-Webdriver: “path is not absolute” error for absolute path

document_name ='TestDoc'
document_path = ("/Users/Me/QA/Project/Documents/#{document_name}")

File.new ("/Users/Me/QA/Project/Documents/#{document_name}") # => File is created
filename_field.send_keys("#{document_path}")
filename_field.send_keys :tab # => To Trigger event but where error occurs

filename_field = browser.file_field(:name, 'file') declared in a module elsewhere.

As far as I can tell, I have provided an absolute path for the filename to upload the file but when the tab key is sent, an error occurs of:

Selenium::WebDriver::Error::UnknownError: unknown error: path is not absolute:

With an odd squiggly symbol in RubyMine that I've never seen before. Any ideas?

Update:

I added

puts filename_field.value 
# => C:\fakepath\TestDoc

Spoke to one of the developers and she said "Browser does it to fake things out, so the filesystem isn't exposed". Not sure if that helps solve my issue or I'm SOL?

I see many small strange things in your code.

Why

document_path = ("/Users/Me/QA/Project/Documents/#{document_name}")

Not

document_path = "/Users/Me/QA/Project/Documents/#{document_name}"

Why

filename_field.send_keys("#{document_path}")

Not

filename_field.send_keys(document_path)

But the main question is why you are using send_keys instead of set ?

I failed to reproduce your problem. Maybe it will be possible if you will provide your html. But i suggest you to try:

filename_field.set(document_path)

Because you can easily check it even with irb send_keys is acting differently in firefox and in chrome for example. So maybe problem with it.

Another suggestion

That is a much more weak idea. But...

Try to clear value before changing it. You can do it with javascript:

b.execute_script("arguments[0].value=''", field)

That error comes from Chromedriver, and comes from sending an incorrect path string to a file element. Since :tab is not a path, it is correctly raising an error.

You shouldn't need to send a tab; just sending the path of the file should accomplish what you need.

I had the same issue with Chromedriver 2.26.436421 and it was solved when I removed the code which was sending the tab key.

With previous Chromedriver sending tab key was required to trigger the change event on the file input but with latest one it seems like it is only causing issues and the change event gets triggered without it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM