java - 无法使用 Xpath 和 CSS 找到元素

标签 java css selenium selenium-webdriver webdriver

我无法使用 XPATH 和 CSS 找到元素

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

1500539792269 geckodriver INFO geckodriver 0.18.0 1500539792279 geckodriver INFO Listening on 127.0.0.1:18947 1500539793062 geckodriver::marionette INFO Starting browser C:\Program Files (x86)\Mozilla Firefox\firefox.exe with args ["-marionette"] 1500539798479 Marionette INFO Listening on port 64608 Jul 20, 2017 2:06:39 PM org.openqa.selenium.remote.ProtocolHandshake createSession INFO: Detected dialect: W3C JavaScript warning: https://login-qa.ihg.com/forms/iam/responsiveV3/js/jquery.min.js, line 1: Using //@ to indicate sourceMappingURL pragmas is deprecated. Use //# instead Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: sidebar-button For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html Build info: version: 'unknown', revision: 'unknown', time: 'unknown'

代码:

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

请帮我解决这个问题

最佳答案

driver.findElement(By.id("sidebar-button")).click();

之前等待一些时间
WebDriverWait wait = new WebDriverWait(driver, 10);
WebElement element = wait.until(
    ExpectedConditions.visibilityOfElementLocated(By.id("sidebar-button")));  

因为第一行有“NoSuchElementException”

关于java - 无法使用 Xpath 和 CSS 找到元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45211047/

相关文章:

python - 错误消息: 'chromedriver' executable needs to be PATH

javascript - 将图像文件添加到 JSON 对象并以 HTML 显示?

html - 为什么只有一个媒体查询不起作用?

JavaScript(用一个按钮打开和关闭某些东西)

java - 如果我不使用selenium服务器独立文件,是否需要selenium服务器使用selenium客户端创建selenium自动化?

java - 在android中扩展dataOutputStream

Java:将 Spring Autowired 库与 Guice 程序结合使用

java : Question regarding immutable and final

java - Eclipse RCP 和 Apache CXF

javascript - 无法使用 Selenium 在自定义滚动条上滚动