【问题标题】:ElementNotInteractableException sending text tn a Search box using Selenium and PythonElementNotInteractableException 使用 Selenium 和 Python 在搜索框中发送文本
【发布时间】:2022-01-04 20:51:02
【问题描述】:

我是这个编码方面的新手,我试图从这个网站访问这个搜索框,但我不断收到错误。 当我在其他网站上尝试相同的东西时,它可以工作,但由于某种原因我不能在这里做。 https://shop.cummins.com/

这是网站:

这是我的代码:

driver = webdriver.Chrome(Browserpath)
website = "https://shop.cummins.com/"
driver.maximize_window()
driver.get(website)

CumminsSearch =driver.find_element_by_xpath("/html/body/div[3]/div[2]/div/div[1]/div/div/c-dbu_home-page-header/div/header/div/div/div[4]/c-dbu_custom-search/div/form/lightning-input[2]/div/input")

time.sleep(10)

CumminsSearch.send_keys("test")

【问题讨论】:

    标签: python selenium xpath css-selectors webdriverwait


    【解决方案1】:
    (//input[@placeholder='Search'])[1]
    

    使用此 xpath 获取唯一标识符。还使用 webdriver 等待以允许元素交互。

    CumminsSearch = wait.until(EC.element_to_be_clickable((By.XPATH,"(//input[@placeholder='Search'])[1]")))
    CumminsSearch.send_keys("test")
    

    进口:

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

    【讨论】:

      【解决方案2】:

      要将字符序列发送到搜索字段,您需要为element_to_be_clickable()引入WebDriverWait,您可以使用以下Locator Strategies之一:

      • 使用CSS_SELECTOR

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "lightning-input[class*='dWindow'] input.slds-input[placeholder='Search']"))).send_keys("Wilbert Balbuena")
        
      • 使用XPATH

        WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//lightning-input[contains(@class, 'dWindow')]//input[@class='slds-input' and @placeholder='Search']"))).send_keys("Wilbert Balbuena")
        
      • 注意:您必须添加以下导入:

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

      【讨论】:

        猜你喜欢
        • 2020-10-30
        • 1970-01-01
        • 2021-04-27
        • 2019-07-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多