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)

'''

  • 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. – CEH Sep 30 '19 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. – robert Roberts Oct 01 '19 at 18:33

1 Answers1

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)


CEH
  • 5,701
  • 2
  • 16
  • 40
  • I tried using javascript to set the value and nothing happens. Code runs but nothing is entered in the input. – robert Roberts Oct 01 '19 at 18:51
  • driver.execute_script("arguments[0].setAttribute('value', 'mCardName')", element) – robert Roberts Oct 01 '19 at 18:52
  • @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? – CEH Oct 01 '19 at 19:03
  • with this script driver.execute_script("arguments[0].setAttribute('value', mCardName);", NameElement) im getting javascript error mCardName is not defined. – robert Roberts Oct 01 '19 at 19:49
  • 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. – robert Roberts Oct 01 '19 at 19:54
  • Could you post the HTML for CC name element you are trying to set? Perhaps there is an issue with the xpath. – CEH Oct 01 '19 at 20:00
  • I posted a link to an image of the HTML code – robert Roberts Oct 02 '19 at 13:41
  • @robertRoberts That is helpful, thank you. Based on the additional context you provided, I wanted to try something. I updated my solution to induce a WebDriver click on the input element, before sending keys. Give this a try. – CEH Oct 02 '19 at 13:48
  • Trying the click event I receive this error: selenium.common.exceptions.ElementClickInterceptedException Message=Message: element click intercepted: Element is not clickable at point (515, -1) (Session info: chrome=77.0.3865.75) – robert Roberts Oct 02 '19 at 15:32
  • @robertRoberts Updated the solution to click with javascript instead of regular .click(). – CEH Oct 02 '19 at 15:34
  • I already tried that and No error but doesnt input the value either – robert Roberts Oct 02 '19 at 16:09
  • @robertRoberts So you've tried Javascript click, followed by selenium send_keys, and there was no error, but nothing happened either? that seems a bit strange, because send_keys will throw an error if the value cannot be sent. – CEH Oct 02 '19 at 16:10
  • Thanks for all your help. I found the issue. I was calling the iframe by xpath class both inputs had different iframes. So i used title instead to call each unique iframe then send the driver. It is working now. – robert Roberts Oct 02 '19 at 16:27
  • That's great news, knew something was fishy but without full HTML it was impossible to see iframe was the issue. Hopefully all of the troubleshooting helped lead you to the root cause. – CEH Oct 02 '19 at 16:30