0

I'm trying to enter OTP in the browser for each of 5 INPUTs. If I type in the codes myself, it jumps to the next INPUT. In Selenium, it stays on one input and tries to enter code in the first INPUT box.

input = browser.find_element(By.XPATH, "//form//input[@type='text']").send_keys("123456")

I want to get all inputs in the list and then enter OTP on each box by the loop.

JeffC
  • 22,180
  • 5
  • 32
  • 55
rerev69417
  • 51
  • 3

2 Answers2

1

Get the elements and then iterate over them:

import time

for element in browser.find_elements(By.XPATH, "//form//input[@type='text']"):
    element.send_keys("some_string\n")
    time.sleep(0.5)
JeffC
  • 22,180
  • 5
  • 32
  • 55
kaliiiiiiiii
  • 925
  • 1
  • 2
  • 21
  • 1
    I would suggest that you put the `browser.find_*` statement inside the loop itself in case the page changes after each entry. Also, you might want to add a 500ms wait after each `.send_keys()` to give the page a chance to process the change since from OP's description, manual entry moves the cursor. It will give the page a chance to run whatever JS it has when INPUT values are changed. – JeffC Feb 05 '23 at 17:27
0

To get all inputs in the list and then enter OTP on each box by the loop you need to use find_elements() to make a list of the <input> elements as follows:

input_elements = browser.find_elements(By.XPATH, "//form//input[@type='text']")
for input_element in input_elements:
    input_element.send_keys("123456")
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352