通过ID定位元素时,方法异常错误-Selenium

时间:2019-10-23 14:50:53

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

我对硒有一个奇怪的问题。 我尝试通过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)
  

***元素信息:{使用= id,值= header-account-logout}方法“:” css选择器

有人可以澄清吗?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

您尝试使用JavascriptExecutor单击吗?可能会起作用。

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