[英]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") 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//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. C:// Username // RubyTutorials是一个已经存在的目录,我希望代码创建一个“ Screenshots”文件夹,然后将图像保存在该文件夹中。
How am I able to do this? 我该怎么做?
I have tried: driver.save_screenshot("C://Username//RubyTutorial//Screenshots//Screenshot.png") 我已经尝试过: 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
保存屏幕截图之前如何创建目录:
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. 正如@ Cagy79指出的那样,您必须使用双反斜杠来转义反斜杠。
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")
). 另外,请记住,如果目录存在,将引发错误,因此,
Dir.mkdir("C:\\\\screenshots") unless Dir.exist?("C:\\\\screenshots")
您可能要检查它(例如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. Dir
类中有很多东西可以使您的生活更轻松。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.