【问题标题】:Selenium : Automating LinkedIn - Profile IconSelenium:自动化 LinkedIn - 个人资料图标
【发布时间】:2015-11-25 08:22:47
【问题描述】:

我是 Selenium 的新手,并尝试使用 Actions 类将鼠标悬停在站点中链接的配置文件图标上,以打开出现在配置文件图像鼠标悬停上的菜单。

下面是我的代码,当它到达这些行时,错误来了:Unable to locate element..

这发生在顶部栏上的所有可用图标(消息/标志图标等)上。

代码:

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();
    }


}

【问题讨论】:

    标签: java selenium-webdriver mouseover


    【解决方案1】:

    您似乎使用了不正确的 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']

    上面的代码我刚刚测试过并且工作正常,所以对你有用。

    【讨论】:

    • 感谢您的回答,连接和消息确实有效,但我仍在为个人资料图标而苦苦挣扎,请帮忙?
    • 我收到“无法找到元素”,因为我使用了正确的 Xpath:WebElement profile = driver.findElement(By.xpath("//*[@id='img-defer-id -1-25469']"));
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    相关资源
    最近更新 更多