[英]Selenium chrome webdriver zoom in using browsercommands
I am in a project that is bounded to use internal webdriverservice which contains some of the webdriver functions.我在一个项目中,该项目必须使用包含一些 webdriver 功能的内部 webdriverservice。
And it's not compatible to build together with org.openqa.selenium.WebDriver;并且不兼容与 org.openqa.selenium.WebDriver 一起构建;
@Component
public class TableauReportingPage {
@Autowired
private BrowserCommands browserCommands;
@Autowired
private WebDriverService webDriverService;
//@Autowired //note this webdriver is comment out,
//import import org.openqa.selenium.WebDriver failed to build under the current project
//private WebDriver webDriver;
public void openExistingResource(String url) throws Exception{
//the method below is working to open the url
browserCommands.goTo(url);
Thread.sleep(40000);
}
public File getScreenshot(){
try {
System.out.println("whats going on??? " + browserCommands.getCurrentUrl());
for(int i=0; i<3; i++) {
browserCommands.findElementBy(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
System.out.println(i);
}
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
//this method below is working to take screenshot,
//however the chrome size is not maximize and tableau report only took half report screenshot size.
//That's why im trying to zoom out chrome browser before taking screenshot.
return browserCommands.getScreenshot();
}
}
this is the piece of code I tried.这是我尝试过的一段代码。
browserCommands.findElementBy(By.tagName("html")).sendKeys(Keys.chord(Keys.CONTROL, Keys.SUBTRACT));
did nothin, it didn't trigger the zoom out.什么也没做,它没有触发缩小。 but the previous piece:但上一篇:
System.out.println("whats going on??? " + browserCommands.getCurrentUrl());
Did print the url that means the object is valid and methods are working properly.确实打印了 url,这意味着 object 有效并且方法工作正常。
What is wrong with the keys.subtract()
? keys.subtract()
有什么问题? why nothing happen and no errors?为什么什么也没发生,也没有错误?
the selenium method page reference selenium 方法页面参考
By the way this is the error I got from using the autowire Webdriver selenium or JavascriptExecutor:顺便说一句,这是我使用 autowire Webdriver selenium 或 JavascriptExecutor 时遇到的错误:
[ERROR] Feature: Data analytics reporting features Time elapsed: 2.081 s <<< ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'tableauReportingPage': Unsatisfied dependency expressed through field 'chromeDriver';原因:org.springframework.beans.factory.UnsatisfiedDependencyException:创建名称为“tableauReportingPage”的bean时出错:通过字段“chromeDriver”表示的依赖关系不满足; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.openqa.selenium.chrome.ChromeDriver' available: expected at least 1 bean which qualifies as autowire candidate.嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.openqa.selenium.chrome.ChromeDriver' available: 预计至少有 1 个 bean 有资格作为自动装配候选者。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)} Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.openqa.selenium.chrome.ChromeDriver' available: expected at least 1 bean which qualifies as autowire candidate.依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} 原因:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有 'org.openqa.selenium.chrome.ChromeDriver 类型的合格 bean ' 可用:预计至少有 1 个 bean 有资格作为 autowire 候选者。 Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}依赖注解:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
You can use JavascriptExecutor for this task,你可以使用 JavascriptExecutor 来完成这个任务,
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.body.style.zoom = '0.90'");
If you need to zoom out multiple times, just loop through as follows.如果您需要多次缩小,只需按以下方式循环即可。
float i = 1;
while(i>0.5) {
i -= 0.1;
System.out.println(i);
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("document.body.style.zoom = '" + i +"'");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.