pythonexcelseleniumdata-driven-tests

How to copy data from excel file and paste into card number textbox in selenium python


I want to copy data from an excel file and paste it into the card number field. card number field does not allow to enter text using the keyboard. it only allows past value.

Is there any way to copy data from an excel file or python file and past it into textbox using selenium python?

Below is the code that I have written.

cardname = self.driver.find_element_by_name(Locators.cardName)
cardname.clear()
cardname.send_keys(datasheet.cell(2, 12).value)

act = ActionChains(self.driver)
act.key_down(Keys.CONTROL).send_keys("a").key_up(Keys.CONTROL).perform()
act.key_down(Keys.CONTROL).send_keys("c").key_up(Keys.CONTROL).perform()
act.send_keys(Keys.TAB).perform()
act.key_down(Keys.CONTROL).send_keys("v").key_up(Keys.CONTROL).perform()

First, I have entered a value in the card name field and then copy that value and perform tab action and then past into the card number field, but it will not work.

This is the HTML code for my textbox that allows only Control + V action.

<input type="text" name="masked_card" class="form-control" value>

I have managed excel files for managing data and reading data from it. I have also tried...

act = ActionChains(self.driver)
cardNumber = self.driver.find_element_by_name(Locators.maskCardNumberField)
act.move_to_element(cardNumber)
act.send_keys(Locators.maskCardNumber)

This is my Website. Legrande

Email:krupal.practice@getnada.com
Password: Test@2020

After login Visit this link to redirect where card number textbox is placed


Solution

  • wait=WebDriverWait(driver,10)                                 
    driver.get('https://ibis-dev.droicelabs.us/login/practice')
    
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[name='email']"))).send_keys("krupal.practice@getnada.com")
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[name='password']"))).send_keys("Test@2020"+Keys.ENTER)
    #Switch this to another wait for element on logged in page
    time.sleep(5)
    driver.get('https://ibis-dev.droicelabs.us/practice/orders/61d7c50335afc005e70aac00/payment/?section=health_insurance')
    

    The main part

    element=wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[name='masked_card']")))
    value='111111111'
    driver.execute_script("arguments[0].setAttribute('value',arguments[1])",element, value)
    

    I was able to switch it with this by setting the value attribute to a string by using driver.execute_script and plugging in the element.

    Imports:

    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait 
    from selenium.webdriver.support import expected_conditions as EC