0

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?

1
  • what id the element id which you are passing ? Commented Oct 23, 2019 at 16:11

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

OK so this means everything is clear now. since it is working OK, but when get exception mentioned css and not id. regards
0

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.