我正在尝试单击付款弹出窗口中的一个元素:
<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}
为什么?希望你能帮助我!
根据错误信息
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
。
错误消息正在自我解释
*** 元素信息:{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"));
看来你们很亲近。虽然您在代码中提到了定位器策略为
By.name
,但是您传递的值:
//label[text()='Expiration Date:']//following::input[@type='tel'][1]
类似于 xpath。因此,您需要使用
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]"))
因为这个选择器是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"