0

Image of HTML code

Im trying to pass values to input elements using Python and selenium ChromeDriver. Im able to set cc # in the first field but for name I get keeping error Element Not Interactable.

I have tried multiple ways to find a solution: -Switching driver to Iframe -Using ActionChains -Using WebDriveWait, element to be visible, element to be clickable -Using Javascript execute script

'''python

    def addCC(mCCNumber,driver,mCardName,mExpiry,mSecCode):      
    # move the driver to the first iFrame 
    #driver.find_elements_by_tag_name("iframe")[0]
    iframe = driver.find_element_by_xpath("//iframe[@class='card-fields-iframe']")
    mainWin = driver.current_window_handle  
    # move the driver to the first iFrame 
    #driver.switch_to_frame(driver.find_elements_by_tag_name("iframe")[0]) 
    driver.switch_to_frame(iframe)
    ccnumber = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,"//input[@placeholder='Card number']")))
    ccnumber.send_keys(mCCNumber)
    #driver.switch_to_window(mainWin)  
    time.sleep(1)

    #cardname = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,"//input[@autocomplete='cc-name']")))
    #WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH ,"//input[@autocomplete='cc-name']")))
    #webdriver.ActionChains(driver).move_to_element(cardname).send_keys(mCardName).perform()
    #webdriver.ActionChains(driver).move_to_element(cardname).click(cardname).send_keys(mCardName).perform()
    #element = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@autocomplete='cc-name']")))
    #driver.execute_script("arguments[0].value= 'dummy dumm';", element)
    element = driver.find_element_by_xpath("//input[@autocomplete='cc-name']")
    webdriver.ActionChains(driver).move_to_element(element).click(element).send_keys(mCardName).perform()
    element.send_keys(mCardName)
    driver.switch_to_window(mainWin)  
    time.sleep(1)
    driver.switch_to_frame(driver.find_elements_by_tag_name("iframe")[0])  
    expdate = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH ,"//input[@id='expiry']"))
            )
    WebDriverWait(driver, 20).until(
            EC.element_to_be_clickable((By.XPATH ,"//input[@id='expiry']"))
            )
    expdate.send_keys(mExpiry)
    driver.switch_to_window(mainWin)  
    time.sleep(1)
    driver.switch_to_frame(driver.find_elements_by_tag_name("iframe")[0])  
    seccode = WebDriverWait(driver, 10).until(
            EC.presence_of_element_located((By.XPATH ,"//input[@placeholder='Security code']"))
            )
    seccode.send_keys(mSecCode)
    driver.switch_to_window(mainWin)  
    time.sleep(3)
    #eles = driver.find_elements_by_xpath('//*[@id]')
    #for ele in eles:
    #    print(ele.tag_name)

'''

2
  • Could you post some HTML of the page you are trying to test, including the iframe element and the CC fields you want to send keys to? This will help track down the issue. Your iframe code looks correct and element not interactable has to do with the element itself. If it were an iframe issue, you would just see webdriver timeout. Commented Sep 30, 2019 at 17:36
  • @Christine Thanks for your reply I have added a link to the Image of the HTML of the page. For context I am able to find and pass CC# okay, but not name on card with out getting error. Commented Oct 1, 2019 at 18:33

1 Answer 1

0

You could try to set the element value using Javascript:

 def addCC(mCCNumber,driver,mCardName,mExpiry,mSecCode):   

    # move the driver to the first iFrame 
    iframe = driver.find_element_by_xpath("//iframe[@class='card-fields-iframe']")

    # save main window context
    mainWin = driver.current_window_handle  

    # move the driver to the first iFrame 
    driver.switch_to_frame(iframe)

    # enter CC number
    ccnumber = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH ,"//input[@placeholder='Card number']")))
    ccnumber.send_keys(mCCNumber)

    time.sleep(1)

    # get CC name element   
    ccNameElement = driver.find_element_by_xpath("//input[@id='name']")

    # click CC name element with JS to activate it
    driver.execute_script("arguments[0].click();", ccNameElement);
    ccNameElement.click()

    # set cc name using send_keys
    ccNameElement.send_keys(mCardName)


Sign up to request clarification or add additional context in comments.

14 Comments

I tried using javascript to set the value and nothing happens. Code runs but nothing is entered in the input.
driver.execute_script("arguments[0].setAttribute('value', 'mCardName')", element)
@robertRoberts I missed a semiclon after setAttribute();. Could you try with driver.executeScript("arguments[0].setAttribute('value', mCardName);", ccNameElement);? Also, can you run print(ccNameElement) to ensure the element is being located correctly?
with this script driver.execute_script("arguments[0].setAttribute('value', mCardName);", NameElement) im getting javascript error mCardName is not defined.
driver.execute_script("arguments[0].setAttribute('value', '" + mCardName + "');", NameElement) Passing the variable like this, I get no error but the value is not set to the input.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.