HTML
<div class="mob-search-box hidden-desk without-sticky">
<form class="form minisearch" id="wizzySearchForm-Mobile" action="/pages/search" method="get" data-gtm-form-interact-id="0">
<div class="wizzy-custom-search-form-wrapper mf-initial wizzy-custom-search-bar">
<div class="wizzy-search-form-mobile-wrapper mf-initial"></div>
<div class="wizzy-search-form mf-initial">
<label for="search" style="display: none;"></label>
<input id="wizzy-search-mobile-input-2" type="search" name="q" placeholder="Search for " class="input-text search-query" spellcheck="false" autocomplete="off" autocorrect="off" autocapitalize="off" style="display: inline-block;" autofocus="" data-gtm-form-interact-field-id="0">
</div>
</div>
</form>
我已经尝试执行以下行
WebElement searchbox = driver.findElement(By.xpath("//div[@class='mob-search-box hidden-desk without-sticky']//div[@class='wizzy-custom-search-form-wrapper mf-initial wizzy-custom-search-bar']//div[@class='wizzy-search-form mf-initial']//input[@id='wizzy-search-mobile-input-2']"));
您正在搜索的INPUT有一个ID。您应该始终使用它。
WebElement searchbox = driver.findElement(By.id("wizzy-search-mobile-input-2"));
如果这不起作用,您需要检查一些事项:
$$("#wizzy-search-mobile-input-2")
。它返回的元素是否超过 1 个?