<dependency> <groupId>org.webjars.npm</groupId> <artifactId>d3</artifactId> <version>5.16.0</version> </dependency> import org.apache.commons.io.FileUtils; import org.apache.commons.lang3.StringUtils; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; import org.openqa.selenium.chrome.ChromeOptions; import org.openqa.selenium.internal.Killable; import org.openqa.selenium.remote.CapabilityType; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.remote.RemoteWebDriver; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.net.URL; public class D3BarChartExample { public static void main(String[] args) { System.setProperty("webdriver.chrome.driver", "path/to/chromedriver"); ChromeOptions options = new ChromeOptions(); WebDriver driver = new ChromeDriver(options); driver.get("https://d3js.org/d3.v5.min.js"); String script = "var dataset = [10, 20, 30, 40, 50]; " + "d3.select('body') " + " .selectAll('div') " + " .data(dataset) " + " .enter() " + " .append('div') " + " .style('height', function(d) { return d * 5 + 'px'; }) " + " .text(function(d) { return d; }) "; ((RemoteWebDriver) driver).executeScript(script); File screenshot = ((RemoteWebDriver) driver).getScreenshotAs(OutputType.FILE); try { FileUtils.copyFile(screenshot, new File("path/to/screenshot.png")); } catch (IOException e) { e.printStackTrace(); } if (driver instanceof Killable) { ((Killable) driver).kill(); } else { driver.quit(); } } }


上一篇:
下一篇:
切换中文