Java:无效选择器:指定了无效或非法的选择器

问题描述 投票:0回答:4

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

<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()='Card Number:']/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}

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

java selenium-webdriver xpath selenide
4个回答
0
投票

根据错误信息

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


0
投票

错误消息正在自我解释

*** 元素信息:{Using=name, value=//div[contains(@class,'buy-tickets')]//div/label[text()='卡片 编号:']/以下兄弟姐妹::输入}

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

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

0
投票

看来你们很亲近。虽然您在代码中提到了定位器策略

By.name
,但是您传递的值:

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

类似于 。因此,您需要使用

By.name
 而不是使用 
By.xpath
,并且可以使用以下任一解决方案:

  • xpath 1使用以下

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

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

0
投票

因为这个选择器是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"
© www.soinside.com 2019 - 2024. All rights reserved.