简体   繁体   中英

Ruby Selenium Screenshot to specific File location

I'm having some trouble with taking a screenshot and saving it to a specific file location:

I having working code which saves a screenshot to the original file location of my workspace:

driver.save_screenshot("screenshot.png")

I want to be able to save this screenshot in its own folder in my C://Username//RubyTutorial//Screenshots//Screenshot.png

C://Username//RubyTutorials is an already existing directory, I would like the code to create a 'Screenshots' folder and then save the image in that folder.

How am I able to do this?

I have tried: driver.save_screenshot("C://Username//RubyTutorial//Screenshots//Screenshot.png")

but I get errors of "No such file or directory"

I have tried different attempts of switching the "//" to "/", "\\" and "\\"

but still no luck.

Thank you :)

Here's a simple, contrived example that demonstrates how to create a directory before saving the screenshot using Dir::mkdir :

Dir.mkdir "C:\\screenshots"
driver.save_screenshot "C:\\screenshots\\Screenshot.png"

As @Cagy79 points out, you have to escape the backslash with a double backslash.

Also, keep in mind that an error will be thrown if the directory exists, so you may want to check for it (eg Dir.mkdir("C:\\\\screenshots") unless Dir.exist?("C:\\\\screenshots") ). And take a look at the documentation. There's a lot in the Dir class that can make your life easier.

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