Selenium Webdriver拖放不适用于google.com

时间:2019-04-26 15:09:43

标签: selenium testing selenium-webdriver automated-tests

不能将 GOOGLE 徽标的图像从 www.google.com 拖到搜索字段。尝试过Acion类,甚至JavascriptExecutor。

        driver.get("https://www.google.com");

//      Element which needs to drag [Google Logo].          
        WebElement from=driver.findElement(By.id("hplogo"));    

//      Element on which need to drop [Google Search-bar].      
        WebElement to=driver.findElement(By.name("q"));

//      Using Action class for drag and drop.       
        Actions act=new Actions(driver);                    

//      Dragged and dropped.        
        act.dragAndDrop(from, to).build().perform();    

/*      JavascriptExecutor _js = (JavascriptExecutor)driver;
        _js.executeScript("$(arguments[0]).simulate('drag-n-drop', 
        {dragTarget:arguments[1],interpolation: 
        {stepWidth:100,stepDelay:50}});", from, to);             
*/

我要拖动并按住Google图片,然后将图片拖放到搜索框中,但没有任何反应。 手动拖放时,在输入提示框中找到了像 https://www.google.com/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png 这样的图像链接。

即使以下代码也可以完美运行,但不适用于google.com!

public class DragAndDrop {
    public static void main(String args[]) throws InterruptedException
    {
        System.setProperty("webdriver.chrome.driver","D:\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("http://jqueryui.com/resources/demos/droppable/default.html");
        Thread.sleep(10000);
        Actions act=new Actions(driver);
        WebElement drag = driver.findElement(By.xpath(".//*[@id='draggable']"));
        WebElement drop = driver.findElement(By.xpath(".//*[@id='droppable']"));
        act.dragAndDrop(drag, drop).build().perform();
    }

1 个答案:

答案 0 :(得分:1)

Try with ROBOT Class, It will helpful.

public class DragAndDrop 
    {
        static WebDriver driver;
        public static void main(String[] args) throws Exception 
        {
            System.setProperty("webdriver.chrome.driver", "F:\\workspace\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();       

            driver.get("http://google.com");
            driver.manage().window().maximize();
            WebElement dragFrom = driver.findElement(By.xpath("//*[@id='abc']"));
            WebElement dragTo = driver.findElement(By.xpath("//*[@id='xyz']"));
            dragAndDropElement(dragFrom, dragTo);
        }
        public static void dragAndDropElement(WebElement dragFrom, WebElement dragTo) throws Exception 
        {
            // Setup robot
            Robot robot = new Robot();
            robot.setAutoDelay(500);
            // Get size of elements
            Dimension fromSize = dragFrom.getSize();
            Dimension toSize = dragTo.getSize();
            Point toLocation = dragTo.getLocation();
            Point fromLocation = dragFrom.getLocation();
            //Make Mouse coordinate centre of element
            toLocation.x += toSize.width/2;
            toLocation.y += toSize.height/2 + 50 ;
            fromLocation.x += fromSize.width/2;
            fromLocation.y += fromSize.height/2 + 50;

            //Move mouse to drag from location
            robot.mouseMove(fromLocation.x, fromLocation.y);
            //Click and drag
            robot.mousePress(InputEvent.BUTTON1_MASK);

            //Drag events require more than one movement to register
            //Just appearing at destination doesn't work so move halfway first
            robot.mouseMove(((toLocation.x - fromLocation.x) / 2) + fromLocation.x , ((toLocation.y - fromLocation.y) / 2) + fromLocation.y);

            //Move to final position
            robot.mouseMove(toLocation.x, toLocation.y);
            //Drop
            robot.mouseRelease(InputEvent.BUTTON1_MASK);
        }
    }