我无法使用 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/