在 selenium 中选择 cssselection 时显示异常

showing exception when selecting the cssselection in selenium

<h3 class="menuheader expandable " headerindex="1h"><span class="accordprefix"></span>

<h3 class="menuheader expandable " headerindex="2h"><span class="accordprefix"></span>
<h3 class="menuheader expandable " headerindex="3h"><span class="accordprefix"></span>

我们的应用程序具有上述 headers。每个 header 都将 headerindex 作为唯一值来标识。我使用了以下代码:

WebElement Tab_selection = driver.findElement(By.cssSelector("<h3>[headerindex=2h]"));
Tab_selection.click();

但显示异常:

invalid selector: An invalid or illegal selector was specified. Could you please help on that

您需要将 2h 括在引号中 (explanation):

h3[headerindex="2h"]

并且,xpath 定位器 始终是一个备选方案:

By.xpath("//h3[@headerindex='2h']")