Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: 指定了无效或非法的选择器

Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified

我想使用部分 css 路径点击按钮:

完整的 xpath:

// /html/body/div[1]/div[2]/div/mat-dialog-container/mobileweb-outbound-pick-picking-dialog/mobileweb-outbound-pick-confirmation/div/div/div/mobileweb-client-area/div/div/div/div[2]/div/div[1]/mobileweb-action-button[1]/div/button
    // //*[@id="source-lp.select-lp.button"]

我试过这个 Java Selenium 网络驱动程序代码:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(20));
    WebElement until = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("//mat-dialog-container/mobileweb-outbound-pick-picking-dialog/mobileweb-outbound-pick-confirmation//*[@id=\"source-lp.select-lp.button\"]")));

但我得到错误:

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.cssSelector: //mat-dialog-container/mobileweb-outbound-pick-picking-dialog/mobileweb-outbound-pick-confirmation//*[@id="source-lp.select-lp.button"] (tried for 20 second(s) with 500 milliseconds interval)

    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:138)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:231)
    at org.mobile.outbound.pick.picking.clusterpicking.ClusterPickingPickTest.buildEmptyClusterFromShipmentId(ClusterPickingPickTest.java:361)
    at org.mobile.outbound.pick.picking.clusterpicking.ClusterPickingPickTest.Mobile_Web_Cluster_Picking_Pick_66732(ClusterPickingPickTest.java:176)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:133)
    at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
    at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
    at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
    at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:824)
    at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
    at java.util.ArrayList.forEach(ArrayList.java:1259)
    at org.testng.TestRunner.privateRun(TestRunner.java:794)
    at org.testng.TestRunner.run(TestRunner.java:596)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:377)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:371)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:332)
    at org.testng.SuiteRunner.run(SuiteRunner.java:276)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1212)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1134)
    at org.testng.TestNG.runSuites(TestNG.java:1063)
    at org.testng.TestNG.run(TestNG.java:1031)
    at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
    at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:109)
Caused by: org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified

你知道我该如何解决这个问题吗?

问题是您指定了 XPath 但使用了 By.cssSelector()

你的第二行应该是

WebElement until = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//mat-dialog-container/mobileweb-outbound-pick-picking-dialog/mobileweb-outbound-pick-confirmation//*[@id=\"source-lp.select-lp.button\"]")));

另请注意,您在问题中输入的 XPath 与您在发布的代码中使用的不同。

从 //html 开始的 XPath 有那么多级别,and/or 使用索引非常脆弱(即使对页面进行很小的更改,它们也很容易损坏)。您应该花一些时间学习如何手工制作定位器以避免此问题。

您拥有的巨大 XPath 可以缩减为 By.id("source-lp.select-lp.button") 而不是 XPath。在转向大型 XPath 之前,始终查找元素上唯一的 ID、名称和其他属性。

您正在看到...

org.openqa.selenium.InvalidSelectorException: invalid selector: An invalid or illegal selector was specified

...作为 you have used is a qualified however within your code trials you considered it as a .

此外,根据您的代码试验,假设元素是 button 元素,id 属性的值为 source-lp.select-lp.button ,您可以使用相对定位器,也可以使用 :

  • xpath:

    //button[@id="source-lp.select-lp.button"]
    
  • cssSelector:

    button[id="source-lp.select-lp.button"]
    

理想情况下, on any clickable element you need to induce for the elementToBeClickable() and you can use either of the following

  • cssSelector:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button[id="source-lp.select-lp.button"]"))).click();
    
  • xpath:

    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[@id="source-lp.select-lp.button"]"))).click();
    

更新

对于 parent1 中的元素:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//parent1//button[@id="source-lp.select-lp.button"]"))).click();

对于 parent2 中的元素:

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//parent2//button[@id="source-lp.select-lp.button"]"))).click();