【问题标题】:Method is incorrect in exception when locating elements by ID - Selenium按 ID 定位元素时异常方法不正确 - Selenium
【发布时间】:2020-02-19 20:42:02
【问题描述】:

我在硒方面遇到了一个奇怪的问题。 我尝试通过 id 定位元素,但是如果它没有找到,异常是关于 css 选择器? 怎么可能?

您可以通过 ID 找到的代码:

  public static void waitForElementSeenAndClick (String elementId)
    {

        WebDriver driver2 = WebDriverMgr.getDriver();

        Wait wait = new FluentWait(driver2)
                .withTimeout(FLUENT_WAIT_MAX, TimeUnit.SECONDS)
                .pollingEvery(FLUENT_WAIT_PULLING, TimeUnit.SECONDS)
                .ignoring(NoSuchElementException.class,
                        Exception.class);

        wait.until(new Function<WebDriver , Boolean>() {
            public Boolean apply (WebDriver driver2) {

                WebElement element = driver2.findElement(By.id(elementId));
                System.out.println("Try to click_1");
                element.click();
                if(element.isDisplayed()) {
                    System.out.println("Try to click_2");
                    element.click();
                    return true;
                }
                System.out.println("Try to click_3");
                return  false;
            }

        });
    }

例外:

Wed Oct 23 17:33:45 IDT 2019:ERROR: no such element: Unable to locate element: {"method":"css selector","selector":"#header\-account\-logout"}
  (Session info: chrome=77.0.3865.120)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'PC', ip: '10.6.6.3', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_65'
Driver info: org.openqa.selenium.chrome.ChromeDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 77.0.3865.120, chrome: {chromedriverVersion: 77.0.3865.40 (f484704e052e0..., userDataDir: C:\Users\AppData\Local...}, goog:chromeOptions: {debuggerAddress: localhost:51842}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: XP, platformName: XP, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 975ad4ceb6ccdfcffd98eea965a5ad99
*** Element info: {Using=id, value=header-account-logout}

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for com.blinkx.rx.ui.common.abstractionLayer.BasePage$3@496a31da (tried for 20 second(s) with 2000 milliseconds interval)

*** 元素信息:{Using=id, value=header-account-logout} 方法":"css 选择器

有人可以澄清一下吗?

【问题讨论】:

  • 你传递的元素id是什么?

标签: selenium selenium-webdriver css-selectors selenium-chromedriver


【解决方案1】:

您是否尝试使用 JavascriptExecutor 点击?也许它会起作用。

JavascriptExecutor executor = (JavascriptExecutor) driver;          
executor.executeScript("arguments[0].click();", lelement);

【讨论】:

    【解决方案2】:

    在开发W3C WebDriver Specification 时,工作组删除了用于通过 ID 查找元素的单独定位器策略。原因是By.id("foo") 在功能上等同于By.cssSelector("#foo")。工作组正确地指出,Selenium 语言绑定可以维护定位器的By.id API,但在幕后将它们转换为正确的 CSS 选择器。这就是为什么即使在通过 ID 查找时,您也会看到有关 CSS 选择器的注释。

    【讨论】:

    • 好的,这意味着现在一切都清楚了。因为它工作正常,但是当得到异常时提到 css 而不是 id。问候
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 2021-11-23
    • 1970-01-01
    • 1970-01-01
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多