如何通过硒查找输入框

时间:2019-02-12 20:33:04

标签: python selenium xpath css-selectors webdriverwait

所以我找不到输入框

HTML:

<div id="w0-find-product"><div class="search-bar" id="w0-find-product-search-bar"><div class="textbox flex-wrapper"><div class="input-wrapper"><input autofocus="" type="text" class="find-product" placeholder="Enter UPC, ISBN, ePID, part number, or product name" role="combobox" aria-owns="w0-find-product-search-bar-autocomplete" aria-expanded="false" aria-autocomplete="list" autocomplete="off" maxlength="100" value="" id="w0-find-product-search-bar-search-field" data-w-onkeydown="checkKey|w0-find-product-search-bar"><span class="clipped" role="status" aria-live="polite" aria-atomic="true" id="w0-find-product-search-bar-search-bar-suggestions-count"></span></div><button class="btn btn--primary btn--large" disabled="" type="button" id="w0-find-product-search-bar-search-button" data-w-onclick="beforeSearch|w0-find-product-search-bar">Get started</button></div></div></div>

下面的Python代码:

SearchBar = browser.find_element_by_xpath('//*[@id="w0-find-product"]')

以下错误:

   Traceback (most recent call last):
  File "C:/Users/Nothing/.PyCharmCE2018.3/config/scratches/sd.py", line 23, in <module>
    SearchBar =browser.find_element_by_xpath('//*[@id="w0-find-product"]')
  File "C:\Users\Nothing\PycharmProjects\UploadBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 394, in find_element_by_xpath
    return self.find_element(by=By.XPATH, value=xpath)
  File "C:\Users\Nothing\PycharmProjects\UploadBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\Users\Nothing\PycharmProjects\UploadBot\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\Nothing\PycharmProjects\UploadBot\venv\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"xpath","selector":"//*[@id="w0-find-product"]"}
  (Session info: chrome=72.0.3626.96)
  (Driver info: chromedriver=73.0.3683.20 (8e2b610813e167eee3619ac4ce6e42e3ec622017),platform=Windows NT 6.1.7601 SP1 x86_64)

3 个答案:

答案 0 :(得分:0)

如果要查找搜索栏本身,请使用browser.find_element_by_id('w0-find-product-search-bar-search-field')

答案 1 :(得分:0)

请尝试使用xpath。两者都可以。

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By



element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//input[@id="w0-find-product-search-bar-search-field"]')))
element.send_keys("1234")

OR

from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC 
from selenium.webdriver.common.by import By

element = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.XPATH, '//input[@class="find-product"]')))
element.send_keys("1234") 

OR

driver.execute_script(("document.getElementById('w0-find-product-search-bar-search-field').value='1234'"))

答案 2 :(得分:0)

首先,根据ChromeDriver - WebDriver for Chrome,您正在使用 chrome = 72 ,您需要下载并使用以下任一二进制文件,但不是 < em> chromedriver=73

接下来,要找到<input>元素,可以使用以下任一解决方案:

  • 使用CSS_SELECTOR

    SearchBar = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.find-product#w0-find-product-search-bar-search-field[placeholder^='Enter UPC']")))
    
  • 使用XPATH

    SearchBar = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='find-product' and @id='w0-find-product-search-bar-search-field'][starts-with(@placeholder, 'Enter UPC')]")))
    
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
  

注意:您可以在Selenium “selenium.common.exceptions.NoSuchElementException” when using Chrome

中找到相关的讨论