Selenium Click()并不总是在chrome或firefox浏览器中工作

时间:2016-12-19 07:32:41

标签: selenium selenium-webdriver click automated-tests

我正在测试我的Office管理站点,并且有一个侧边栏包含多个选项但是点击方法不起作用,我想点击员工链接但不点击,它工作正常直到 driver.findElement(By.xpath(".//*[@id='sidebar']/div/ul/li[2]/a/span[1]")).click();

但之后点击方法无效请帮忙 ScreenShot is here

我的代码:

        WebDriver driver =new ChromeDriver();
        driver.get("http:******");
        driver.manage().window().maximize();

        driver.findElement(By.xpath(".//*[@id='UserName']")).sendKeys("username");
        driver.findElement(By.xpath(".//*[@id='Password']")).sendKeys("password");
        driver.findElement(By.xpath(".//*[text()='Submit']")).click();
        driver.findElement(By.xpath(".//*[@id='sidebar-collapse']")).click();
        driver.findElement(By.xpath(".//*[@id='sidebar-collapse']")).click();

        driver.findElement(By.xpath(".//*[@id='sidebar']/div/ul/li[2]/a/span[1]")).click();
        driver.findElement(By.xpath(".//*[@id='sidebar']/div/ul/li[2]/ul/li[3]/a/span[1]")).click();

2 个答案:

答案 0 :(得分:0)

尝试在驱动程序初始化后添加隐式等待

WebDriver driver =new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

答案 1 :(得分:0)

试试这个

WebDriverWait wait = new WebDriverWait(tester, 15);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='sidebar']/div/ul/li[2]/ul/li[3]/a/span[1]")));
driver.findElement(By.xpath(".//*[@id='sidebar']/div/ul/li[2]/ul/li[3]/a/span[1]")).click();