1. 程式人生 > >selenium.common.exceptions.InvalidSelectorException

selenium.common.exceptions.InvalidSelectorException

頁面元素如下

<input type="text" maxlength="20" class="form-control search-input">

在selenium中使用如下方式取元素會報錯

WebElement searchInput = driver.findElement(By.className("form-control search-input"));
selenium.common.exceptions.InvalidSelectorException

原因:class 屬性值中間的空格表示的是後代選擇器 , 如上表示的是“form-control”內的“search-input”,改為如下方式即可

WebElement searchInput = driver.findElement(By.className("search-input"));