selenium.common.exceptions.ElementNotInteractiableException:消息:无法将元素滚动到视图中,该视图使用selenium与输入交互

2024-10-03 21:29:47 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个网站,我需要填写一个输入字段。 只有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\u元素\u by\u 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

我使用了预期条件&;WebdriverWait与element_to_be_clickable()一起,但在2分钟内找不到它

我还尝试:

actions.move_to_element(element).perform()

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

enter image description here字段的图像

HTML的图像: HTML

HTML


Tags: textselfidinputexecutebydriverselenium
1条回答
网友
1楼 · 发布于 2024-10-03 21:29:47

此错误消息

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

…意味着在调用click()WebElement不可交互


解决方案

理想情况下,要单击元素,您需要为element_to_be_clickable()诱导WebDriverWait,并且可以使用以下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
    

相关问题 更多 >