屏幕截图未保存在 Selenium 的目录中
The screenshot does not get saved in the directory in Selenium
所以我正在尝试截取网页屏幕截图的代码。我使用 class 生成随机字符串值,然后将这些值分配给文件名。然而,当我 运行 代码时,它会完全执行,但是屏幕截图永远不会保存到目录中。
代码:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenShot {
private WebDriver driver;
private String BaseUrl;
@Before
public void setUp() throws Exception {
BaseUrl = "https://www.flock.co";
System.setProperty("webdriver.chrome.driver", "C:\Automation\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(BaseUrl);
Thread.sleep(5000);
WebElement emailField = driver.findElement(By.xpath("//div[@id='main-area']//input"));
WebElement Button = driver.findElement(By.xpath("//div[@id='main-area']//button"));
emailField.sendKeys("incomeplete");
Button.click();
}
public static String getRandomString(int length) {
StringBuilder sb = new StringBuilder();
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for (int i = 0; i < length; i++) {
int index = (int) (Math.random() * characters.length());
sb.append(characters.charAt(index));
}
return sb.toString();
}
@After
public void tearDown() throws Exception {
String fileName = getRandomString(10) + ".png";
String directory = "C:\Users\farzan.s.DIRECTI\Desktop\LetsKodeIt";
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(directory+fileName));
driver.quit();
}
}
但是我删除了这个随机字符串生成器并直接在 FileUtils 中指定目录路径,它可以工作。
代码:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenShot {
private WebDriver driver;
private String BaseUrl;
@Before
public void setUp() throws Exception {
BaseUrl = "https://www.flock.co";
System.setProperty("webdriver.chrome.driver", "C:\Automation\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(BaseUrl);
Thread.sleep(5000);
WebElement emailField = driver.findElement(By.xpath("//div[@id='main-area']//input"));
WebElement Button = driver.findElement(By.xpath("//div[@id='main-area']//button"));
emailField.sendKeys("incomeplete");
Button.click();
}
@After
public void tearDown() throws Exception {
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(C:\Users\farzan.s.DIRECTI\Desktop\LetsKodeIt\jfjfnfh.png));
driver.quit();
}
}
您在路径中遗漏了“\\”。
应该是
FileUtils.copyFile(sourceFile, new File(directory+"\"+fileName));
所以我正在尝试截取网页屏幕截图的代码。我使用 class 生成随机字符串值,然后将这些值分配给文件名。然而,当我 运行 代码时,它会完全执行,但是屏幕截图永远不会保存到目录中。
代码:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenShot {
private WebDriver driver;
private String BaseUrl;
@Before
public void setUp() throws Exception {
BaseUrl = "https://www.flock.co";
System.setProperty("webdriver.chrome.driver", "C:\Automation\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(BaseUrl);
Thread.sleep(5000);
WebElement emailField = driver.findElement(By.xpath("//div[@id='main-area']//input"));
WebElement Button = driver.findElement(By.xpath("//div[@id='main-area']//button"));
emailField.sendKeys("incomeplete");
Button.click();
}
public static String getRandomString(int length) {
StringBuilder sb = new StringBuilder();
String characters = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
for (int i = 0; i < length; i++) {
int index = (int) (Math.random() * characters.length());
sb.append(characters.charAt(index));
}
return sb.toString();
}
@After
public void tearDown() throws Exception {
String fileName = getRandomString(10) + ".png";
String directory = "C:\Users\farzan.s.DIRECTI\Desktop\LetsKodeIt";
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(directory+fileName));
driver.quit();
}
}
但是我删除了这个随机字符串生成器并直接在 FileUtils 中指定目录路径,它可以工作。 代码:
import java.io.File;
import java.util.concurrent.TimeUnit;
import org.apache.commons.io.FileUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
public class ScreenShot {
private WebDriver driver;
private String BaseUrl;
@Before
public void setUp() throws Exception {
BaseUrl = "https://www.flock.co";
System.setProperty("webdriver.chrome.driver", "C:\Automation\chromedriver_win32\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
}
@Test
public void test() throws Exception {
driver.get(BaseUrl);
Thread.sleep(5000);
WebElement emailField = driver.findElement(By.xpath("//div[@id='main-area']//input"));
WebElement Button = driver.findElement(By.xpath("//div[@id='main-area']//button"));
emailField.sendKeys("incomeplete");
Button.click();
}
@After
public void tearDown() throws Exception {
File sourceFile = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(sourceFile, new File(C:\Users\farzan.s.DIRECTI\Desktop\LetsKodeIt\jfjfnfh.png));
driver.quit();
}
}
您在路径中遗漏了“\\”。 应该是
FileUtils.copyFile(sourceFile, new File(directory+"\"+fileName));