无法移出ViewPort窗格 - Selenium

时间:2018-03-06 20:36:41

标签: selenium-webdriver

我正在尝试自动化网页" http://the-internet.herokuapp.com/exit_intent"如果我们从Viewpane移出页面顶部,则弹出窗口会显示一条消息。但是我无法在selenium的帮助下做到这一点,我们有什么方法可以执行此操作吗?

2 个答案:

答案 0 :(得分:1)

您可以尝试以下代码:这是使用Robot类

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Test;
import java.awt.*;

public class stackOverFlowQs {
    @Test
    public void test() throws InterruptedException, AWTException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://the-internet.herokuapp.com/exit_intent");
        Thread.sleep(3000);
        Robot robot = new Robot();
        robot.mouseMove(600,0);
        Thread.sleep(3000);
        driver.findElement(By.xpath(".//*[@id='ouibounce-modal']/div[2]/div[3]/p")).click();
    }
}

这是使用操作:

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.Test;

public class stackOverFlowQs {
    @Test
    public void test() throws InterruptedException {
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("http://the-internet.herokuapp.com/exit_intent");
        WebElement e = driver.findElement(By.cssSelector("h3"));
        Actions action = new Actions(driver);
        action.moveToElement(e).moveByOffset(600,-1).build().perform();
        driver.findElement(By.xpath(".//*[@id='ouibounce-modal']/div[2]/div[3]/p")).click();
    }
}

答案 1 :(得分:0)

对于我来说,这正在使用webrowser Chrome进行:

Actions action=new Actions(driver);
action.moveByOffset(600, -1).build().perform();

这对我不起作用:

action.moveToElement(e).moveByOffset(600,-1).build().perform();