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"));

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章