Krupal Vaghasiya
Krupal Vaghasiya

Reputation: 550

How to select textbox value and move to the another textbox using selenium python

I want to enter the value in one textbox and then select all values and after I want to move that value in another textbox.

enter image description here

In the above image, I want to move the selected value into the Card Number textbox. Because card number textbox is not allowed to type using the keyboard. it only allows to Past the value. I have already asked a question but unfortunately did not get any help.

This is my asked question

Upvotes: 2

Views: 772

Answers (1)

The Amateur Coder
The Amateur Coder

Reputation: 839

It looks like the input's oninput method makes it reset its value. Changing/overriding it did it for me:

driver.execute_script('''function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
cnoinput = getElementByXpath("/html/body/div/div/div/div[1]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div[1]/input");
cnoinput.oninput = function(){this.value = "0000 0000 0000 0000"};
cnoinput.removeAttribute('value');
''')

cnoinput is the input element. Changing what would happen when something's typed into it, by changing its oninput function, that changes the input's value to "0000 0000 0000 0000" will prevent the default behaviour of the element and will only keep changing its value to "0000 0000 0000 0000".

Replace that function with what you'd like to do (paste the copied value from Excel). I got the getElementByXpath(path) function from here.

This is the full code:

from selenium import webdriver
from selenium.webdriver.common.by import By
import time
import pyautogui
#Imports

driver = webdriver.Chrome(executable_path=r"chromedriver.exe")
driver.get("https://ibis-dev.droicelabs.us/login/practice")


email = ''
while not email:
    try:
        email = driver.find_element(By.NAME, 'email') #Get the Email input
    except:
        continue

#Wait till the input is loaded, but I think this method will be faster than WebDriverWait. 
#https://stackoverflow.com/a/53023604/16136190

email.click()

pyautogui.write("[email protected]") #"write" or type the Email

pwd = ''
while not pwd:
    try:
        pwd = driver.find_element(By.NAME, 'password')
    except:
        continue

pwd.click()
pyautogui.write("Test@2020")

login = ''
while not login:
    try:
        login = driver.find_element(By.XPATH, "//input[@value='SIGN IN']")
    except:
        continue

login.click()

time.sleep(2) #Assuming the time to log in is less than 2 seconds. If not, increase it.
driver.get("https://ibis-dev.droicelabs.us/practice/orders/61d7c50335afc005e70aac00/payment/?section=health_insurance")
#Because the login details are already stored, just load the page.

cardno = ''
while not cardno:
    try:
        cardno = driver.find_element(By.XPATH, "/html/body/div/div/div/div[1]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div[1]/input")
    except:
        continue

cardno.click()

driver.execute_script('''function getElementByXpath(path) {
  return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
cnoinput = getElementByXpath("/html/body/div/div/div/div[1]/div/div[2]/div[1]/div/div/div/div/div/div[2]/div[1]/input");
cnoinput.oninput = function(){this.value = "0000 0000 0000 0000"}; // Change this function.
cnoinput.removeAttribute('value'); //Trigger oninput.
''')

Upvotes: 1

Related Questions