phantomjs替换为无头chromedriver时出现错误

时间:2019-01-31 06:16:29

标签: java selenium selenium-webdriver selenium-chromedriver

我遇到了很多有关js的错误...所以我改成无头的chromedriver,它对于截图特定元素的效果更好,但在其他截图代码中却出现了错误

Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387) 

on port 44903
Only local connections are allowed.
janv. 31, 2019 7:02:22 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
    at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
    at java.awt.image.BufferedImage.getSubimage(Unknown Source)
    at CarteEtdInfo.photoProfile(CarteEtdInfo.java:57)
    at Accueil.<init>(Accueil.java:99)
    at Login$2.actionPerformed(Login.java:287)
    at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
    at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
    at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
    at javax.swing.AbstractButton.doClick(Unknown Source)
    at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(Unknown Source)
    at javax.swing.SwingUtilities.notifyAction(Unknown Source)
    at javax.swing.JComponent.processKeyBinding(Unknown Source)
    at javax.swing.KeyboardManager.fireBinding(Unknown Source)
    at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
    at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
    at javax.swing.JComponent.processKeyBindings(Unknown Source)
    at javax.swing.JComponent.processKeyEvent(Unknown Source)
    at java.awt.Component.processEvent(Unknown Source)
    at java.awt.Container.processEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
    at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
    at java.awt.Component.dispatchEventImpl(Unknown Source)
    at java.awt.Container.dispatchEventImpl(Unknown Source)
    at java.awt.Window.dispatchEventImpl(Unknown Source)
    at java.awt.Component.dispatchEvent(Unknown Source)
    at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
    at java.awt.EventQueue.access$500(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.awt.EventQueue$3.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.awt.EventQueue$4.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
    at java.awt.EventQueue.dispatchEvent(Unknown Source)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
    at java.awt.EventDispatchThread.run(Unknown Source)

这是在CarteEtdInfo类中出错的代码

public void photoProfile() throws IOException {

    String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));

    Login webpage = new Login();
    WebDriver pagee = webpage.driver;
    pagee.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?Idsession="+cookie+"&action1=toCarteEtd");


    // Get entire page screenshot
    WebElement taswira = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[4]/td[1]/div[2]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/img[1]"));
    File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    BufferedImage fullImg = null;
    try {
        fullImg = ImageIO.read(screenshot);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    // Get the location of element on the page
    org.openqa.selenium.Point point = taswira.getLocation();

    // Get width and height of the element
    int eleWidth = taswira.getSize().getWidth();
    int eleHeight = taswira.getSize().getHeight();

    // Crop the entire page screenshot to get only element screenshot
    BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); // line 57
    try {
        ImageIO.write(eleScreenshot, "png", screenshot);
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    // Copy the element screenshot to disk
    File screenshotLocation = new File("temp\\avatar.png");
    try {
        FileUtils.copyFile(screenshot, screenshotLocation);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

这是第57行

        BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);

Login类中一个元素的屏幕快照的先前代码工作正常,那么为什么这个有问题?

Chromedriver不是无头的也可以,但无头的不是

2 个答案:

答案 0 :(得分:2)

无头chrome的默认窗口大小可能小于预期的大小,这可能会导致您的元素在窗口之外,因此不可见。您可以自己设置寡妇的大小,也可以使用以下方法最大化窗口:

$base_directory = dirname(__FILE__);
echo $base_directory; //'/home/myProject/public_html/admin/app/controlers/'

答案 1 :(得分:1)

我通过添加

解决了该问题
chromeOptions.addArguments("window-size=1980,960");