我正在尝试使用带有Selenium的AShot来进行屏幕截图。
require 'selenium/webdriver'
require 'javalib/ashot-1.5.4.jar'
driver_path = 'C:ProgramDatachocolateylibchromedrivertoolschromedriver.exe'
Selenium::WebDriver::Chrome::Service.driver_path = driver_path
base_url = 'https://www.google.com/'
driver = Selenium::WebDriver.for :chrome
driver.navigate.to base_url
ashot = Java::RuYandexQatoolsAshot::AShot.new
png = ashot.takeScreenshot(driver)
test_pod_name = 'c:datagoogle_screenshot.png'
File.write(test_pod_name, png)
puts "Writing to: #{test_pod_name}"
driver.close
但我收到了这个错误消息:
Unhandled Java exception: java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
java.lang.NoClassDefFoundError: org/openqa/selenium/WebDriver
getDeclaredMethods0 at java/lang/Class.java:-2
privateGetDeclaredMethods at java/lang/Class.java:2729
getDeclaredMethods at java/lang/Class.java:2003
computeValue at org/jruby/javasupport/binding/Initializer.java:467
computeValue at org/jruby/javasupport/binding/Initializer.java:464
getFromHashMap at java/lang/ClassValue.java:227
getFromBackup at java/lang/ClassValue.java:209
get at java/lang/ClassValue.java:115
getMethods at org/jruby/javasupport/binding/Initializer.java:498
setupClassMethods at org/jruby/javasupport/binding/ClassInitializer.java:116
initialize at org/jruby/javasupport/binding/ClassInitializer.java:31
setupProxyClass at org/jruby/javasupport/binding/Initializer.java:100
createProxyClass at org/jruby/javasupport/Java.java:528
generateClassProxy at org/jruby/javasupport/Java.java:486
createProxyClassForClass at org/jruby/javasupport/Java.java:442
computeValue at org/jruby/javasupport/JavaSupportImpl.java:126
computeValue at org/jruby/javasupport/JavaSupportImpl.java:118
get at org/jruby/util/collections/MapBasedClassValue.java:25
getProxyClassFromCache at org/jruby/javasupport/JavaSupportImpl.java:199
getProxyClass at org/jruby/javasupport/Java.java:409
getProxyClass at org/jruby/javasupport/Java.java:402
getProxyClassOrNull at org/jruby/javasupport/Java.java:976
getProxyClassOrNull at org/jruby/javasupport/Java.java:927
getProxyOrPackageUnderPackage at org/jruby/javasupport/Java.java:885
relativeJavaClassOrPackage at org/jruby/javasupport/JavaPackage.java:188
const_missing at org/jruby/javasupport/JavaPackage.java:127
call at org/jruby/javasupport/JavaPackage$INVOKER$i$1$0$const_missing.gen:-1
finvoke at org/jruby/RubyClass.java:909
invoke at org/jruby/runtime/Helpers.java:442
callMethod at org/jruby/RubyBasicObject.java:363
searchModuleForConst at org/jruby/ir/targets/ConstantLookupSite.java:133
<main> at C:/src/energyflow/exdb/data/netlogix/scripts/google-screenshot-selenium.rb:12
invokeWithArguments at java/lang/invoke/MethodHandle.java:627
load at org/jruby/ir/Compiler.java:94
runScript at org/jruby/Ruby.java:830
runNormally at org/jruby/Ruby.java:749
runNormally at org/jruby/Ruby.java:767
runFromMain at org/jruby/Ruby.java:580
doRunFromMain at org/jruby/Main.java:417
internalRun at org/jruby/Main.java:305
run at org/jruby/Main.java:232
main at org/jruby/Main.java:204
Caused by:
java.lang.ClassNotFoundException: org.openqa.selenium.WebDriver
findClass at java/net/URLClassLoader.java:382
loadClass at java/lang/ClassLoader.java:418
loadClass at java/lang/ClassLoader.java:351
getDeclaredMethods0 at java/lang/Class.java:-2
privateGetDeclaredMethods at java/lang/Class.java:2729
getDeclaredMethods at java/lang/Class.java:2003
computeValue at org/jruby/javasupport/binding/Initializer.java:467
computeValue at org/jruby/javasupport/binding/Initializer.java:464
getFromHashMap at java/lang/ClassValue.java:227
getFromBackup at java/lang/ClassValue.java:209
get at java/lang/ClassValue.java:115
getMethods at org/jruby/javasupport/binding/Initializer.java:498
setupClassMethods at org/jruby/javasupport/binding/ClassInitializer.java:116
initialize at org/jruby/javasupport/binding/ClassInitializer.java:31
setupProxyClass at org/jruby/javasupport/binding/Initializer.java:100
createProxyClass at org/jruby/javasupport/Java.java:528
generateClassProxy at org/jruby/javasupport/Java.java:486
createProxyClassForClass at org/jruby/javasupport/Java.java:442
computeValue at org/jruby/javasupport/JavaSupportImpl.java:126
computeValue at org/jruby/javasupport/JavaSupportImpl.java:118
get at org/jruby/util/collections/MapBasedClassValue.java:25
getProxyClassFromCache at org/jruby/javasupport/JavaSupportImpl.java:199
getProxyClass at org/jruby/javasupport/Java.java:409
getProxyClass at org/jruby/javasupport/Java.java:402
getProxyClassOrNull at org/jruby/javasupport/Java.java:976
getProxyClassOrNull at org/jruby/javasupport/Java.java:927
getProxyOrPackageUnderPackage at org/jruby/javasupport/Java.java:885
relativeJavaClassOrPackage at org/jruby/javasupport/JavaPackage.java:188
const_missing at org/jruby/javasupport/JavaPackage.java:127
call at org/jruby/javasupport/JavaPackage$INVOKER$i$1$0$const_missing.gen:-1
finvoke at org/jruby/RubyClass.java:909
invoke at org/jruby/runtime/Helpers.java:442
callMethod at org/jruby/RubyBasicObject.java:363
searchModuleForConst at org/jruby/ir/targets/ConstantLookupSite.java:133
<main> at C:/src/energyflow/exdb/data/netlogix/scripts/google-screenshot-selenium.rb:12
invokeWithArguments at java/lang/invoke/MethodHandle.java:627
load at org/jruby/ir/Compiler.java:94
runScript at org/jruby/Ruby.java:830
runNormally at org/jruby/Ruby.java:749
runNormally at org/jruby/Ruby.java:767
runFromMain at org/jruby/Ruby.java:580
doRunFromMain at org/jruby/Main.java:417
internalRun at org/jruby/Main.java:305
run at org/jruby/Main.java:232
main at org/jruby/Main.java:204
有趣的是,错误转储没有引用AShot,但错误源于安装AShot的行。
Selenium webdriver方法中有一个用于捕获屏幕截图的开放方法。你可以使用类似的东西
@driver.save_screenshot('c:datagoogle_screenshot.png')