【问题标题】:Java: invalid selector: An invalid or illegal selector was specifiedJava:无效选择器:指定了无效或非法的选择器
【发布时间】:2020-02-16 20:50:39
【问题描述】:

我正在尝试点击支付弹出窗口中的一个元素:

<label data-v-533987c6="" xpath="1">Card Number:</label>
<input data-v-533987c6="" type="tel" data-mask="#### #### #### ####" data-previous-value="" xpath="1">

使用以下 xPath:

@FindBy(name = "//div[contains(@class,'buy-tickets')]//div/label[text()='卡号:']/following-sibling::input")

但我收到此错误:

org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified
  (Session info: chrome=80.0.3987.87)
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/invalid_selector_exception.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'DESKTOP-VQ56FMV', ip: '192.168.0.13', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_231'
Driver info: org.openqa.selenium.remote.RemoteWebDriver
Capabilities {acceptInsecureCerts: false, browserName: chrome, browserVersion: 80.0.3987.87, chrome: {chromedriverVersion: 80.0.3987.106 (f68069574609..., userDataDir: C:\Users\Nastya\AppData\Loc...}, goog:chromeOptions: {debuggerAddress: localhost:64958}, javascriptEnabled: true, networkConnectionEnabled: false, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, proxy: Proxy(), setWindowRect: true, strictFileInteractability: false, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify, webdriver.remote.sessionid: 3ca3f388f2b3cf47642733a6f03...}
Session ID: 3ca3f388f2b3cf47642733a6f0383c1e
*** Element info: {Using=name, value=//div[contains(@class,'buy-tickets')]//div/label[text()='Card Number:']/following-sibling::input}

为什么?希望你能帮帮我!

【问题讨论】:

  • 导致问题的选择器不是您发布的选择器,而是消息在最后一行报告的选择器 (//div[contains(@class,'buy-tickets')]/ /div/label[text()='Card Number:']/following-sibling::input} ) 您能否确认您实际上使用的是使用 XPath 提取的代码,而不是使用 CSS 选择器?

标签: java selenium selenium-webdriver xpath selenide


【解决方案1】:

根据错误提示

Element info: {Using=name, value=//div[contains(@class,'buy-tickets')]//div/label[text()='Card Number:']/following-sibling::input}

您正在尝试使用另一个xpath

//div[contains(@class,'buy-tickets')]//div/label[text()='Card Number:']/following-sibling::input`

By.name,无效。您需要使用By.xpath

【讨论】:

  • 伙计们,你们是最棒的!!非常感谢!:)
【解决方案2】:

错误信息正在自我解释

*** 元素信息:{Using=name, value=//div[contains(@class,'buy-tickets')]//div/label[text()='Card 编号:']/following-sibling::input}

您正在使用 xpath 但选择器正在使用 name

driver.findElement(By.xpath("//label[contains(text(),'Card Number')]/following-sibling::input"));

【讨论】:

    【解决方案3】:

    你似乎很亲密。尽管您在代码中将Locator Strategy 称为By.name,但是您传递的值:

    //label[text()='Expiration Date:']//following::input[@type='tel'][1]
    

    类似于。因此,您需要使用 By.xpath 而不是使用 By.name,您可以使用以下任一解决方案:

    • xpath 1 使用 以下

      driver.findElement(By.xpath("//label[text()='Card Number:']//following::input[1]"))
      
    • xpath 2 使用 following-sibling

      driver.findElement(By.xpath("//label[text()='Card Number:']//following-sibling::input[1]"))
      

    【讨论】:

      【解决方案4】:

      因为这个选择器是 XPath 您可以通过 css、xpath、名称等找到一个元素 直接替换

      @FindBy(name = "//div[contains(@class,'buy-tickets')]//div/label[text()='Card Number:']/following-sibling::input"
      

      @FindBy(xpath = "//div[contains(@class,'buy-tickets')]//div/label[text()='Card Number:']/following-sibling::input"
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        • 2019-06-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-28
        • 2015-06-18
        相关资源
        最近更新 更多