0

With Python Selenium how can I click on the increment or decrement buttons of an Input type number. The increment and decrement buttons are Browser specific objects and not part of the DOM. So I can not use the inspector to get the xpath. I want to increment and decrement these inputs.

HTML:

<input type="number" name="qty" value="69">

Edge: enter image description here

Firefox: enter image description here

Since they do not appear in the DOM inspector (for either browser) I don't see how to make the click action for those buttons:

driver.find_element_by_xpath('/html/body/div[2]/main/form/div[1]/table/tbody/tr[1]/td[3]/input[1]').click()
Mattman85208
  • 1,858
  • 2
  • 29
  • 51

1 Answers1

0

You can remove the existing attribute through removeAttribute() set back the new attribute using setAttribute() as follows:

counter = driver.find_elements(By.CSS_SELECTOR, "input[name='qty'][type='number']")
driver.execute_script("arguments[0].removeAttribute('value')", counter);
driver.execute_script("arguments[0].setAttribute('value','50')", counter)

In a single line:

driver.execute_script("arguments[0].setAttribute('value','50')", driver.find_elements(By.CSS_SELECTOR, "input[name='qty'][type='number']"))

Note : You have to add the following imports :

from selenium.webdriver.common.by import By
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • Is it not possible to click on the increment and or decrement buttons? (with Selenium) – Mattman85208 Mar 17 '22 at 21:36
  • I think the increment and or decrement buttons are **not** buttons as such but integral part of a control. However, the key to all the questions/assumptions may be found in it's parent/ancestor elements. – undetected Selenium Mar 17 '22 at 21:40