我试图找到一个元素,但我收到错误元素未找到我的代码:
Dim bot As WebDriver
Set bot = New WebDriver
bot.Start "chrome"
bot.Get "http://..."
bot.FindElementByName("userName").SendKeys "text"
bot.FindElementByName("password").SendKeys "text"
bot.FindElementByClass("ButtonLast").Click
bot.FindElementByName("searchField").SendKeys "text"
Stop
HTML:
<input
class="SearchRoundCorner"
onblur="searchFieldInnerTxt(this);"
onfocus="searchFieldInnerTxt(this);"
style="width: 150px; padding-left: 20px; color: rgb(128, 128, 128);"
type="text"
name="searchField"
value="search"
title=""
onkeypress="if ((window.event && window.event.keyCode == 13)|| event.which==13){ document.ListView.pageNo.value=1;document.ListView.saveSelection.value = 0;submitForm(document.ListView);return false; }"
/>
它工作得很好,直到我得到line-bot.FindElementByName("searchField")。SendKeys"text">
错误:
Error message:Run-Time Error 7 NoSuchElementError Element not found for name=searchfield
所需的元素是一个动态元素,因此要向元素发送字符序列,您可以使用以下定位器策略之一:
使用FindElementByName:
bot.FindElementByName("searchField").SendKeys "text"
使用<<li>em> FindElementByCss :
bot.FindElementByCss("input.SearchRoundCorner[name='searchField'][value='search']").SendKeys "text"
使用<<li>em> FindElementByXPath :
bot.FindElementByXPath("//input[@class='SearchRoundCorner' and @name='searchField'][@value='search']").SendKeys "text"