【问题标题】:Unable to click on the logout link using selenium webdriver 3.0.1使用 selenium webdriver 3.0.1 无法单击注销链接
【发布时间】:2016-11-03 08:00:02
【问题描述】:

我已经编写了以下代码以在登录后从 http://www.quikr.com/ 注销。但是当我使用 Firefox 浏览器运行以下代码时,代码运行没有任何问题,但 webdriver 无法单击注销链接。

能否请您提出相同的建议。

注意:同样的代码在 chrome 浏览器上也能正常工作。

我正在使用:

Firefox 版本: 49.0.1 Chrome 版本: 54.0.2840.59 m Selenium 版本: 3.0.1 操作系统: Win10 64 位 Java: 1.8

public static void doLogout(){          
            WebDriverWait wait = new WebDriverWait(driver, 5);          
            wait.until(ExpectedConditions.elementToBeClickable(By.xpath(".//*[@id='responsiveHeader']/div[1]/div[1]/ul/li[4]/a/i"))).click();
            wait.until(ExpectedConditions.elementToBeClickable(By.linkText("Sign Out"))).click();
}

【问题讨论】:

标签: java selenium selenium-webdriver webdriver


【解决方案1】:

退出链接仅在滚动下拉菜单后可见。

First Rule: For clicking on element - that element should be visible on screen.
Second Rule: To make it visible you need to scroll to that element.

为此,在打开用户下拉菜单后写下下面的代码。

WebElement ele = driver.findElement(By.xpath(".//*[@id='responsiveHeader']/div[1]/div[1]/ul/li[4]/ul/li[9]/a")); // Sign Out element 
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].scrollIntoView(true);", ele); // scrolling upto "Sign Out" element.
// Quikr has some problem of scrolling in website - so, we have to again scroll to top.
js.executeScript("scroll(0, 0);");
driver.findElement(By.linkText("Sign Out")).click();

此代码运行良好。我已经正确测试了。如果你喜欢它并且它对你很有效,那么接受Answer

【讨论】:

  • 感谢 Yojna 的解决方案。但是我有一个问题,代码可以正常工作,而无需在 chrome 中显式滚动到元素。这怎么可能?
  • chrome driver和firefox中的javascript执行方式不同。我可以看到在 chrome 驱动程序中没有滚动下拉 signOut 是可见的。因此,无需滚动 chrome 驱动程序。您的代码应该可以完美地在所有浏览器上运行。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-11-06
  • 1970-01-01
  • 2017-02-03
  • 2016-09-29
  • 2013-10-04
相关资源
最近更新 更多