使用Selenium WebDriver首次点击IE元素

时间:2017-09-14 05:47:18

标签: selenium internet-explorer

我正在尝试点击IE 11中的链接,并使用以下代码:

driver.findElement(By.xpath("//a[text()='En savoir plus']")).click();

我没有得到任何例外,但页面没有在任何地方导航,它也会冻结整个页面,我无法继续。

几年前我遇到了同样的问题,我记得的解决方案是两次使用相同的命令:

driver.findElement(By.xpath("//a[text()='En savoir plus']")).click();
driver.findElement(By.xpath("//a[text()='En savoir plus']")).click();

这将成功点击链接而不冻结页面。

这个问题有解决办法吗?

2 个答案:

答案 0 :(得分:2)

使用javascript executor方法尝试以下代码。

注意: - 在点击此按钮之前,请提供几秒wait,以便您的驱动程序可以找到webelement

对于wait我正在使用Explicit Wait方法。

new WebDriverWait(driver, 60).until(ExpectedConditions.elementToBeClickable(driver.findElement(By.xpath("//a[text()='En savoir plus']"))));                 //wait for 60 seconds.
WebElement button = driver.findElement(By.xpath("//a[text()='En savoir plus']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", button);

答案 1 :(得分:1)

也许这会有所帮助?

public static BufferedImage scaleBilinear(BufferedImage before, double scale) {
    final int interpolation = AffineTransformOp.TYPE_BILINEAR;
    return scale(before, scale, interpolation);
}

public static BufferedImage scaleBicubic(BufferedImage before, double scale) {
    final int interpolation = AffineTransformOp.TYPE_BICUBIC;
    return scale(before, scale, interpolation);
}

public static BufferedImage scaleNearest(BufferedImage before, double scale) {
    final int interpolation = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
    return scale(before, scale, interpolation);
}

@NotNull
private static 
BufferedImage scale(final BufferedImage before, final double scale, final int type) {
    int w = before.getWidth();
    int h = before.getHeight();
    int w2 = (int) (w * scale);
    int h2 = (int) (h * scale);
    BufferedImage after = new BufferedImage(w2, h2, before.getType());
    AffineTransform scaleInstance = AffineTransform.getScaleInstance(scale, scale);
    AffineTransformOp scaleOp = new AffineTransformOp(scaleInstance, type);
    scaleOp.filter(before, after);
    return after;
}

/**
 * This is a more generic solution. It produces the same result, but it shows how you 
 * can draw anything you want into the newly created image. It's slower 
 * than scaleBilinear().
 * @param before The original image
 * @param scale The scale factor
 * @return A scaled version of the original image
 */
private static BufferedImage scale2(BufferedImage before, double scale) {
    int w = before.getWidth();
    int h = before.getHeight();
    // Create a new image of the proper size
    int w2 = (int) (w * scale);
    int h2 = (int) (h * scale);
    BufferedImage after = new BufferedImage(w2, h2, before.getType());
    AffineTransform scaleInstance = AffineTransform.getScaleInstance(scale, scale);
    AffineTransformOp scaleOp
            = new AffineTransformOp(scaleInstance, AffineTransformOp.TYPE_BILINEAR);

    Graphics2D g2 = (Graphics2D) after.getGraphics();
    // Here, you may draw anything you want into the new image, but we're just drawing
    // a scaled version of the original image. This is slower than 
    // calling scaleOp.filter().
    g2.drawImage(before, scaleOp, 0, 0);
    g2.dispose();
    return after;
}

/**
 * I call this one "questionable" because it uses the questionable getScaledImage() 
 * method. This method is no longer favored because it's slow, as my tests confirm.
 * @param before The original image
 * @param scale The scale factor
 * @return The scaled image.
 */
private static Image questionable(final BufferedImage before, double scale) {
    int w2 = (int) (before.getWidth() * scale);
    int h2 = (int) (before.getHeight() * scale);
    return before.getScaledInstance(w2, h2, Image.SCALE_FAST);
}