【问题标题】:How to "hover over" the button in selenium? [duplicate]如何“悬停”硒中的按钮? [复制]
【发布时间】:2015-01-30 06:10:10
【问题描述】:

如何为元素添加悬停?考虑我下面的代码:

package AutomationFramework;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.*;

import org.openqa.selenium.firefox.FirefoxDriver;

public class LoginPage {

    private static WebDriver driver = null;

    public static void main(String[] args) {

        // Create a new instance of the Firefox driver

        driver = new FirefoxDriver();

        //Put a Implicit wait

        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        //Launch the Website

        driver.get("URL");

        // Find the element that's ID attribute is 'account'(My Account) 

       // driver.findElement(By.xpath("/html/body/table/tbody/tr[5]/td/table/tbody/tr/td[2]/form/table/tbody/tr[3]/td[2]/input")).click();

        // Enter Username on the element 

        driver.findElement(By.name("userName")).sendKeys("remote"); 

        // Find the element that's ID attribute is 'pwd' (Password)

        driver.findElement(By.name("password")).sendKeys("aaaaaa");

        // Now submit the form. WebDriver will find the form for us from the element 

        driver.findElement(By.name("submit")).click();

        // Print a Log In message to the screen

        System.out.println(" Login Successfully");
        driver.findElement(By.name("img2")).click();

        // Find the element that's ID attribute is 'account_logout' (Log Out)

        //driver.findElement (By.name("img104")).click();

        // Close the driver

        driver.quit();

            }

    }

【问题讨论】:

  • 您希望将鼠标悬停在哪个元素上?请说明。

标签: java selenium selenium-webdriver hover


【解决方案1】:

使用Action 类。

Actions hover = new Actions(driver);

WebElement Elem_to_hover = driver.findElementBy(By.id("id"));

hover.moveToElement(Elem_to_hover);

hover.build();

hover.perform();

【讨论】:

    【解决方案2】:

    Shruthi,请参考以下示例程序以使用 selenium web 驱动程序进行鼠标悬停:

    public class mhover {
    
    public static void main(String[] args){
    
        WebDriver driver = new FirefoxDriver();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.get("http://www.google.com");
        WebElement ele = driver.findElement(By.id("gbqfba"));
        Actions action = new Actions(driver);
        action.moveToElement(ele).build().perform();
    
    
        }
    
    }
    

    如果您有任何疑问,请告诉我。

    【讨论】:

    • 谢谢你。它工作正常
    猜你喜欢
    • 2020-12-16
    • 2019-09-07
    • 2020-04-27
    • 2017-09-07
    • 2013-09-01
    • 2018-05-25
    • 2015-03-20
    • 1970-01-01
    相关资源
    最近更新 更多