0

I have a site and I need to fill an input field. Only the field with 37.75 is causing a problem, it is not disabled it has a placeholder and I can interact with it easily myself, but when it comes to selenium, I can't.

I have tried:

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

This did nothing

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))

Every find_element_by_id() returned the exception:

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

I used Expected Conditions & WebdriverWait with element_to_be_clickable() but it couldn't find it in 2 minutes

I also tried:

actions.move_to_element(element).perform()

and

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

enter image description here Image of the field

Image of the HTML: HTML

HTML

Alper
  • 13
  • 4

1 Answers1

1

This error message...

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

...implies that the WebElement wasn't interactable when you invoked click() on it.


Solution

Ideally, to click on the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using 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))
    
  • Using 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))
    
  • Note: You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
DebanjanB
  • 118,661
  • 30
  • 168
  • 217
  • I used Expected Conditions & WebdriverWait with element_to_be_clickable but it couldn't find it in 2 minutes. I used XPATH and ID, btw the elements was clearly visible and I could click it with my mouse while it couldn't find with expected conditions – Alper Dec 26 '20 at 20:58
  • @Alper Here's what you can try. Chcek if scroll is needed to make the element appear in the window. If not that. Open developer tools. press CTRL+F check if typing the `xpath` shows the element, else your xpath is wrong. It may be right, but sometimes, scrollintoview is neccessary. Without looking at the website it's hit and miss. – Abhishek Rai Dec 26 '20 at 21:05
  • @AbhishekRai Thanks for your help, my xpath is correct and I forgot to mention in the question that I also tried scrollIntoView, and actions.move_to_element(element).perform() but I still can't proceed – Alper Dec 26 '20 at 21:11
  • is there a `shadow-root` above your element? Did you check if it is a different `iframe`? Else, right click and copy the `full` xpath`. That's all I can think of. – Abhishek Rai Dec 26 '20 at 21:12
  • @Alper checkout the updated answer and let me know the status – DebanjanB Dec 26 '20 at 21:14
  • @DebanjanB I guess I can't see the updated answer, it is still the same suggesting Expected Conditions – Alper Dec 26 '20 at 21:19
  • @AbhishekRai I checked and neither of those exist, I tried the full xpath and no changes, thanks for taking your time to help – Alper Dec 26 '20 at 21:20
  • @Alper Well. Check the expert's updated answer..and let us know..if it doesn't work. I think he updated his answer with your element names. – Abhishek Rai Dec 26 '20 at 21:22
  • As @AbhishekRai mentioned your locator strategy was hitting the wrong element but **not** the desired element. The element mentioned in the error doesn't matches the element in the snapshot. The XPath needs to be changed. Hence I dropped the idea of using `ID` and included the parent `` – DebanjanB Dec 26 '20 at 21:24
  • when you open developer tools..press CTRL+F and type `//input[@id="product_length"]` It selects the element? That is the `xpath` – Abhishek Rai Dec 26 '20 at 21:26
  • @DebanjanB Oh, I pressed CTRL+F in dev tools and it returned 8 of the same id input elements! – Alper Dec 26 '20 at 21:32
  • @Alper so you need to select the index of the elements found. For e.g answer =driver.find_elements_by_xpath(xpath). answer[index here].send_keys('whatever you want to send)...type of thing..You get it? – Abhishek Rai Dec 26 '20 at 21:37
  • @Alper How did you get it though? Write here. Will help future visitors a lot. Mention the mistake you were making, if any. That is what SO is about. Answer your own question, in fact. – Abhishek Rai Dec 26 '20 at 21:45