2

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

Krupal Vaghasiya
  • 536
  • 7
  • 21
  • You can paste the copied value using [PyAutoGUI](https://pyautogui.readthedocs.io/en/latest/install.html). Install it using [`pip install pyautogui`](https://pypi.org/project/PyAutoGUI/) and use `pyautogui.write(copied_value)` to paste it. (First, select the card number textbox using selenium) – The Amateur Coder Jan 08 '22 at 05:34
  • @TheAmateurCoder Can you please describe how to copy value and how to write using `PyAutoGUI`? – Krupal Vaghasiya Jan 08 '22 at 05:43
  • I tried pasting that value but I could not even copy paste any number into that card number text_field. – Rajagopalan Jan 08 '22 at 05:44
  • @KrupalVaghasiya as Rajagopalan said even i am not able to write or paste anything in card number. i think you are using react so my i guess is you have not write handling the input tag in front end correctly please check that . – gaurav Jan 08 '22 at 05:57
  • I posted an answer on the previous question for you. – Arundeep Chohan Jan 08 '22 at 05:59
  • @ArundeepChohan it works but it seems like we are violating something when we do through Javascript `setAttribute` – Rajagopalan Jan 08 '22 at 07:39
  • @KrupalVaghasiya very sorry; I didn't test it then. PyAutoGUI won't help. I tried @Arundeep Chohan's answer; it didn't work for me either. But one of my methods worked; after trying the methods one by one, I finally found that `input.removeAttribute("value");` (using `driver.execute_script()` and getting the input with its XPath) works, but the value is changed back in a flash, probably due to the element's properties. I finally tried and found another method that changes it. It's just a workaround. – The Amateur Coder Jan 09 '22 at 15:29

1 Answers1

1

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("krupal.practice@getnada.com") #"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.
''')
The Amateur Coder
  • 789
  • 3
  • 11
  • 33
  • When I execute your code test will pass but it does not enter the value in the card number filled. – Krupal Vaghasiya Jan 10 '22 at 04:36
  • @KrupalVaghasiya, what do you mean? Very sorry if I've misunderstood anything. [It changes the value](https://ufile.io/hgwo48hv)... – The Amateur Coder Jan 10 '22 at 04:47
  • It will not enter value in the textbox but still test is passed. – Krupal Vaghasiya Jan 10 '22 at 04:53
  • @KrupalVaghasiya, what exactly do you mean by "test is passed"? Sorry, I'm new to software testing. The code changes the `input`'s `value` attribute. You can check using DevTools' inspect element. It has the value attribute and doesn't get reset even on user input. – The Amateur Coder Jan 10 '22 at 05:31
  • Can you please help me on this question? [https://stackoverflow.com/questions/70651274/returning-wrong-data-from-excel-when-i-am-trying-to-read-data-from-an-excel-file] – Krupal Vaghasiya Jan 10 '22 at 12:03