我正在嘗試使用 selenium 搜索網頁。下面是搜索頁面的 HTML 代碼片段。
<div class="cmp-globalsite-search ac_box">
<input id="searchString" type="text" class="form-control searchString
ac_input" placeholder="Search" aria-label="Search" required="" autocomplete="off">
<input type="hidden" id="searchTargetUrl" value="/en/search">
<a class="searchHead btn" href="#">
<span class="nav-icon gcom-icon-search"></span>
</a>
</div>
以上取自https://www.gartner.com.
我正在使用下面的 python 代碼來使用搜索頁面。
url = 'https://www.gartner.com/'
driver = webdriver.Chrome(executable_path='path to chrome web driver')
driver.get(url)
elem = driver.find_element_by_id("SearchString")
elem.send_keys("Risk Management Solution")
elem.send_keys(keys.RETURN)
奇怪的是我收到以下錯誤:
NoSuchElementException: no such element: Unable to locate element: {"method":"css
selector","selector":"[id="SearchString"]"}
(Session info: chrome=97.0.4692.71)
我也在下面嘗試過:
elem = driver.find_element_by_xpath("""//input[@id="searchTargetUrl"]/input[@value = "Risk Management Solution""")
得到類似的錯誤:
InvalidSelectorException: invalid selector: Unable to locate an element with the xpath
expression //input[@id="searchTargetUrl"]/input[@value = "Risk Management Solution because
of the following error:
SyntaxError: Failed to execute 'evaluate' on 'Document': The string
'//input[@id="searchTargetUrl"]/input[@value = "Risk Management Solution' is not a valid
XPath expression.
(Session info: chrome=97.0.4692.71)
我是硒的新手,因此尋求任何線索。
更新
我已經full xpath從上面的網站復制了搜索頁面。
//*[@id="gartnerinternalpage-9ac4556e05"]/div[2]/div/div/div/div/div/div/section/div/div/div[1]/div[2]/div/div[1]/input
所以我在python代碼中替換了上面的xpath字串并添加了 //input[@value={0}]
還是沒有運氣!!
uj5u.com熱心網友回復:
默認情況下,搜索欄位不可見,要將字符序列發送到搜索欄位,您需要先在搜索圖示上
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/415052.html
標籤:
下一篇:AttributeError:“NoneType”物件沒有屬性“find_all”(BeautifulSoup)(ChromeDriver)
