【问题标题】:selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view interacting with input using Seleniumselenium.common.exceptions.ElementNotInteractableException:消息:无法将元素滚动到使用 Selenium 与输入交互的视图中
【发布时间】:2020-12-26 20:44:31
【问题描述】:

我有一个网站,我需要填写一个输入字段。 只有 37.75 的字段会导致问题,它没有被禁用,它有一个占位符,我可以自己轻松地与之交互,但是当涉及到 selenium 时,我不能。

我试过了:

self.driver.execute_script(f"document.getElementById('product_length').value='{str(depth10)}'")

这没有任何作用

pyperclip.copy(str(depth10))
self.driver.find_element_by_id("product_length").click()
pclip.paste()
self.driver.find_element_by_id("product_length").send_keys(str(depth10))

每个 find_element_by_id() 都返回异常:

selenium.common.exceptions.ElementNotInteractableException: Message: Element <input id="product_length" class="input-text wc_input_decimal" name="_length" type="text"> could not be scrolled into view

我在 element_to_be_clickable() 中使用了预期条件和 WebdriverWait,但在 2 分钟内找不到它

我也试过了:

actions.move_to_element(element).perform()

driver.execute_script("arguments[0].scrollIntoView();", element)

现场图片

HTML 图像:

HTML

【问题讨论】:

    标签: python selenium selenium-webdriver webdriver webdriverwait


    【解决方案1】:

    此错误消息...

    selenium.common.exceptions.ElementNotInteractableException: Message: Element could not be scrolled into view
    

    ...暗示当您在其上调用 click() 时,WebElement 不可交互。


    解决方案

    理想情况下,点击你需要诱导WebDriverWaitelement_to_be_clickable()的元素,你可以使用以下Locator Strategies之一:

    • 使用CSS_SELECTOR

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "span.wrap > input#product_length[name='variable_length[1]']"))).send_keys(str(depth10))
      
    • 使用XPATH

      WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[@class='wrap']/input[@id='product_length' and @name='variable_length[1]']"))).send_keys(str(depth10))
      
    • 注意:您必须添加以下导入:

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

    【讨论】:

    • 我使用了预期条件和 WebdriverWait 和 element_to_be_clickable,但在 2 分钟内找不到它。我使用了 XPATH 和 ID,顺便说一句,元素清晰可见,我可以用鼠标单击它,而在预期条件下找不到它
    • @Alper 您可以尝试以下方法。检查是否需要滚动才能使元素出现在窗口中。如果不是这样。打开开发者工具。按 CTRL+F 检查输入 xpath 是否显示元素,否则您的 xpath 错误。这可能是正确的,但有时,scrollintoview 是必要的。不用看网站,它就会被击中和错过。
    • @AbhishekRai 感谢您的帮助,我的 xpath 是正确的,我忘了在问题中提到我也尝试过 scrollIntoView 和 actions.move_to_element(element).perform() 但我仍然不能继续
    • 您的元素上方是否有shadow-root?你检查它是否是不同的iframe?否则,右键单击并复制 full xpath`。我能想到的就这些了。
    • @Alper 检查更新的答案并让我知道状态
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 2019-12-20
    • 2022-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多