尽管使用了正确的搜索 ID,如何使用 selenium 在网页中进行搜索

How to search in a webpage using selenium despite using correct search id

我正在尝试使用 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}]

仍然没有运气!!

默认情况下,搜索字段不可见,要将字符序列发送到搜索字段,您需要 on the search icon first inducing WebDriverWait for the and you can use either of the following :

  • 使用CSS_SELECTOR:

    driver.get("https://www.gartner.com/en")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button#onetrust-accept-btn-handler"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.nav-icon.gcom-icon-search"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input#searchString"))).send_keys("Risk Management Solution" + Keys.RETURN)
    
  • 使用 XPATH:

    driver.get("https://www.gartner.com/en")
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='onetrust-accept-btn-handler']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='nav-icon gcom-icon-search']"))).click()
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@id='searchString']"))).send_keys("Risk Management Solution" + Keys.RETURN)
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  • 浏览器快照: