Bastian
Bastian

Reputation: 1237

Method is incorrect in exception when locating elements by ID - Selenium

I have an odd issue in selenium. I try to locate element by id, however if it is not located the exception is about css selector? how it can be?

Code you can see find by 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;
            }

        });
    }

Exception:

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)

*** Element info: {Using=id, value=header-account-logout} method":"css selector

Can someone please clarify?

Upvotes: 0

Views: 185

Answers (2)

JimEvans
JimEvans

Reputation: 27496

When the W3C WebDriver Specification was developed, the working group removed the separate locator strategy for finding elements by ID. The reason is that By.id("foo") is functionally equivalent to By.cssSelector("#foo"). The working group rightly pointed out that the Selenium language bindings could maintain the By.id API for locators, but translate them to the correct CSS selector under the covers. That is why you’re seeing a note about CSS selectors even when finding by ID.

Upvotes: 1

suba
suba

Reputation: 1440

Did you try to click with JavascriptExecutor? May be it will work.

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

Upvotes: 0

Related Questions