【问题标题】:TakesScreenshot cannot be resolved to a type in Selenium WebDriverTakesScreenshot 无法解析为 Selenium WebDriver 中的类型
【发布时间】:2016-05-27 19:22:26
【问题描述】:

我想截取网页的屏幕截图。 我写了 2 行代码,但我得到“无法将 TakesScreenshot 解析为类型”。请在下面找到我的代码。查找文件 f1、scrFile 或 scrFile1。我尝试了所有 3 仍然错误仍然存​​在。

import java.io.File;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

public class Radio {

    public static void main(String[] args) {
        WebDriver driver=new FirefoxDriver();
        driver.get("http://echoecho.com/htmlforms10.htm");

        File f1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(f1, "E:\\Pessoal\\QTPSelenium\\Screenshot1.jpg");

        File scrFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(scrFile, new File("File Location\\File Name"),true);

        File scrFile1 = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        // Now you can do whatever you need to do with it, for example copy somewhere
        FileUtils.copyFile(scrFile1, new File("c:\\tmp\\screenshot.png"));

        driver.manage().window().maximize();
        List<WebElement> allRadios= driver.findElements(By.name("group1"));
        System.out.println("Total -->"+allRadios.size());

        System.out.println("Before");
        System.out.println(allRadios.get(0).getAttribute("checked"));
        System.out.println(allRadios.get(1).getAttribute("checked"));       
        System.out.println(allRadios.get(2).getAttribute("checked"));
        allRadios.get(0).click();
        System.out.println("After");
        System.out.println(allRadios.get(0).getAttribute("checked"));
        System.out.println(allRadios.get(1).getAttribute("checked"));       
        System.out.println(allRadios.get(2).getAttribute("checked"));



    }

}

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    你需要使用import org.openqa.selenium.TakesScreenshot;

    【讨论】:

    • 我仍然得到同样的错误。 “屏幕截图无法解析为类型”
    • @MohammedSajjad 我编辑了我的答案,将您的旧代码与TakesScreenshot 一起使用并添加import org.openqa.selenium.TakesScreenshot;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-30
    • 2017-04-24
    • 1970-01-01
    相关资源
    最近更新 更多