Selenium Webdriver拖放不适用于google.com

问题描述 投票:0回答:1

无法将www.google.com上的GOOGLE徽标图片拖到“搜索”字段中。尝试使用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);             
*/

我想拖动谷歌图像,然后想要将图像拖放到搜索框,但没有任何反应。手动如果我拖放,我在输入搜索框中找到像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();
    }
selenium testing selenium-webdriver automated-tests
1个回答
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);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.