運行硒python腳本時出現錯誤-self._execute(self._execute(Command.SEND_KEYS_TO_ELEMENT,)


0

我在python中使用硒,我嘗試單擊下面的元素。但是每次顯示錯誤(啟動時都起作用)時,只會在此行出現錯誤,並嘗試了CSS選擇器ID和所有HTML代碼::

<div class="uploadWrap">
                                                <input type="text" class="uploadInput ng-pristine ng-untouched ng-invalid ng-invalid-required" name="net_qty" ng-model="cauction.net_sell_qty" required="" required-param="true" valid-number="">
                                                <label class="upload-btn">MT</label>
                                            </div>

Selenium code::

quantity = wait.until(EC.presence_of_element_located((By.NAME, 'net_qty')))
quantity.send_keys('105')

Error ::

Traceback (most recent call last):
  File "C:/Users/Devesh/PycharmProjects/E-mandi/jj.py", line 76, in <module>
    quantity.send_keys('105')

Error::
    self._execute(Command.SEND_KEYS_TO_ELEMENT,
    return self._parent.execute(command, params)
    self.error_handler.check_response(response)
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable
  (Session info: chrome=81.0.4044.122)
  (Driver info: chromedriver=2.43.600210 (68dcf5eebde37173d4027fa8635e332711d2874a),platform=Windows NT 10.0.18363 x86_64)
0

First, try with basic sleep:

//wait for 20 second
time.sleep(20)

// fetch the element
input = driver.find_element(By.XPATH,"//input[@name='net_qty']")

 // send value
 input.sendKeys(1);

If this work, it means the pages taking some time to load that's why it was failing. Then you can change time.sleep() into explicit wait using expected condition visbilityof.

If this doesn't work, read on:

The exception says element not visible:

so the first possibility is that the element is not in the viewport, means you cannot see element unless you scroll to that element. In this case use below code:

// fetch the element
input = driver.find_element(By.XPATH,"//input[@name='net_qty']")

// run JS to reveal the element   
driver.execute_script("arguments[0].scrollIntoView();",input);

// send file path keys
input.sendKeys(1);

The second possibility is that some other element may be hiding the element.

// fetch the element
input = driver.find_element(By.XPATH,"//input[@name='net_qty']")

// run JS to reveal the element   
driver.execute_script("arguments[0].style.display = 'block';", input);

// send file path keys
input.sendKeys(1);

Also, you can replace display value to the original value once the value is send:

// fetch the element
input = driver.find_element(By.XPATH,"//input[@name='net_qty']")

// display property will designed whether to bring 
// the element to front or keep in backround 
// we will save the current value to an element
display=driver.execute_script("return arguments[0].style.display", input);

// run JS to bring the element to foreground by setting display to block  
driver.execute_script("arguments[0].style.display = 'block';", input);

// send value
input.sendKeys(1);

//now replace the display value to the previous one: 
driver.execute_script("arguments[0].style.display = arguments[1];", input, display);