【问题标题】:Unable to find element using Xpath and CSS无法使用 Xpath 和 CSS 找到元素
【发布时间】:2017-07-20 09:42:42
【问题描述】:

我无法使用 XPATH 和 CSS 找到 Element

我使用 firefox 执行代码,我尝试了所有方法来查找此处提到的元素,但总是出现下面提到的错误。

1500539792269 geckodriver INFO geckodriver 0.18.0 1500539792279 geckodriver INFO 监听 127.0.0.1:18947 1500539793062 geckodriver::marionette INFO 启动浏览器 C:\Program 带参数的文件 (x86)\Mozilla Firefox\firefox.exe ["-marionette"] 1500539798479 Marionette INFO 监听端口 64608 2017 年 7 月 20 日 下午 2:06:39 org.openqa.selenium.remote.ProtocolHandshake createSession 信息:检测到的方言:W3C JavaScript 警告: https://login-qa.ihg.com/forms/iam/responsiveV3/js/jquery.min.js,线 1:不推荐使用 //@ 指示 sourceMappingURL 杂注。采用 //# 而不是线程“main”中的异常 org.openqa.selenium.NoSuchElementException:无法定位元素: sidebar-button 有关此错误的文档,请访问:http://seleniumhq.org/exceptions/no_such_element.html 构建信息: 版本:“未知”,修订:“未知”,时间:“未知”

代码:

driver.findElement(By.id("sidebar-button")).click();
Thread.sleep(2000);
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//*[@id='reservations']/a"));
action.moveToElement(we).build().perform();

请帮我解决这个问题

【问题讨论】:

  • 请分享 HTML sn-p。
  • geckdriver 似乎有问题,您是否在使用其他浏览器或其他 firefox 或 geck 版本时收到此错误。你能检查一下吗?

标签: java css selenium selenium-webdriver webdriver


【解决方案1】:

driver.findElement(By.id("sidebar-button")).click();之前给一些等待时间

WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("sidebar-button")));  

当您在第一行得到“NoSuchElementException”时

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2020-06-14
    • 2020-04-09
    • 1970-01-01
    • 1970-01-01
    • 2018-09-20
    相关资源
    最近更新 更多