【问题标题】:.click() and .send_keys() methods not being recognized Python Selenium.click() 和 .send_keys() 方法未被识别 Python Selenium
【发布时间】:2021-07-20 19:52:36
【问题描述】:

我在 Python 中使用 Selenium 来单击文本输入字段并将一些文本写入其中。 .click().send_keys() 方法都没有被识别。有人可以帮忙吗?

另外,有没有办法阻止 Selenium 自动打印到控制台?我的程序是基于控制台的,Selenium 正在向我提供的 input() 写入内容,因为它会打印到控制台。

这是一个代码sn-p:

url = "https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65"
browser = webdriver.Edge("Web Scrapers\msedgedriver.exe")
browser.get(url)
textbox = browser.find_element_by_id("LocationSearch_input")
textbox.click()
textbox.send_keys(zipcode)
textbox.send_keys(Keys.ENTER)

【问题讨论】:

  • 你能提供minimal reproducible example看看你做了什么吗?
  • @Kshitz,我编辑了问题
  • 您遇到的错误/问题是什么?
  • 你需要等待它出现
  • @JeremyKahan 它被立即渲染,他试图抓住一个隐藏的元素是问题

标签: python html selenium


【解决方案1】:

你可以试试explicitWait 希望这对你有用

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@type='text']"))).send_keys("20874",Keys.RETURN)

【讨论】:

    【解决方案2】:

    我建议你通过显式等待来做到这一点。

    我给出了这个答案,因为没有一个答案真正使用带有 ID 属性的显式等待:

    driver.maximize_window()
    driver.implicitly_wait(30)
    driver.get("https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65")
    wait = WebDriverWait(driver, 10)
    wait.until(EC.element_to_be_clickable((By.ID, "LocationSearch_input"))).send_keys('60007' + Keys.RETURN)
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CurrentConditions--dataWrapperInner']"))).click() 
    

    进口:

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

    【讨论】:

      【解决方案3】:

      据我所知,这里一切正常,但是您将 textbox 变量指向了错误的 HTML 元素。

      LocationSearch_input 是不直接附加到搜索框的隐藏标签。我会尝试将其指向

      SearchInput--SearchInput--M7lKl SearchInput--enableSearchIcon--1Ugsx,父元素之一。

      【讨论】:

      • .click() 和 .send_keys() 方法仍然无法识别,这就是问题所在。
      • 请详细说明“无法识别”,您的输出是否有错误?还是只是没有点击。
      • 好吧,在 Visual Studio Code 中它并没有显示方法存在,因为它们是白色的。
      • 你是在顶部导入硒吗?如果是这样,不要依赖你的代码编辑器来告诉你什么存在什么不存在,总是使用文档。不要专注于你的编辑器显示的内容,重要的是:代码是否运行?会发生什么?
      • ElementNotInteractableException: 元素
        无法通过键盘访问
      【解决方案4】:

      至少在 Firefox 中进行测试,这是一个时间问题。点击有效。元素不可交互(无法通过键盘访问)的错误来自 send_keys 行。如果我们在 click() 之后等待,则该元素可以通过键盘访问。

      以下内容可能会被改进(真的不喜欢睡觉,但有时它会起作用),但对我有用:

      url = "https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65"
      browser.get(url)
      browser.find_element_by_id('LocationSearch_input').click()
      time.sleep(5)
      browser.find_element_by_id('LocationSearch_input').send_keys('Boston')
      

      此时,您需要点击 10 个选项中的任何一个才是您真正想要的。

      【讨论】:

        猜你喜欢
        • 2021-11-21
        • 2022-11-13
        • 2016-10-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-11-06
        相关资源
        最近更新 更多