我是Selenium的新手,并尝试使用Actions类将鼠标悬停在站点中链接上可用的Profile图标上,以打开出现在Profile Image的Mouseover上的菜单。

下面是我的代码,到达这些行时出现错误:无法找到元素。

这是通过顶部栏上的链接上可用的所有图标(消息/标记图标等)发生的。

代码:

public class LinkedIn {
    WebDriver driver = new FirefoxDriver();
    @BeforeTest
    public void setUp() throws Exception {

        String baseUrl = "http://www.linkedin.com/";
        driver.get(baseUrl);

    }


    @Test
    public void login() throws InterruptedException
    {
        WebElement login = driver.findElement(By.id("login-email"));
        login.sendKeys("*****@gmail.com");

        WebElement pwd = driver.findElement(By.id("login-password"));
        pwd.sendKeys("*****");


        WebElement in = driver.findElement(By.name("submit"));
        in.click();

        Thread.sleep(10000);
    }


     @Test
        public void profile()  {
    // here it gives error to me : Unable to locate element
        Actions action = new Actions(driver);
        WebElement profile = driver.findElement(By.xpath("//*[@id='img-defer-id-1-25469']"));
        action.moveToElement(profile).build().perform();
          driver.quit();
    }


}

最佳答案

看来您使用了不正确的xpath,请检查以下示例将鼠标悬停在“消息”按钮上:

            Thread.sleep(5000);
            Actions action = new Actions(driver);
            WebElement profile = driver.findElement(By.xpath("//*[@id='account-nav']/ul/li[1]"));
            action.moveToElement(profile).build().perform();


正确的Xpath是:

对于消息图标:"//*[@id='account-nav']/ul/li[1]"

对于连接图标://*[@id='dropdowntest']

上面的代码我刚刚测试过并且可以正常工作,因此将为您工作。

09-20 07:06