如何修复错误Python Selenium Chromdriver中不可交互的元素

时间:2019-09-30 17:31:10

标签: python selenium iframe selenium-chromedriver

Image of HTML code

我正在尝试使用Python和Selenium ChromeDriver将值传递给输入元素。 我无法在第一个字段中设置cc#,但由于名称的原因,我一直保持错误“元素不可交互”。

我尝试了多种方法来找到解决方案: -将驱动程序切换到iframe -使用动作链 -使用WebDriveWait,元素可见,元素可点击 -使用Javascript执行脚本

'''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)

'''

1 个答案:

答案 0 :(得分:0)

您可以尝试使用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)