Unable to locate element of credit card number using selenium python

走远了吗. 提交于 2019-11-27 09:50:30

As you are trying to send a character sequence within an <input> field sems to be a Credit Card Number and historically Credit Card Number resides within <iframes>.

So if the the desired elements are within an <iframe> so you have to:

  • Induce WebDriverWait for the desired frame to be available and switch to it.
  • Induce WebDriverWait for the desired element to be clickable.
  • You can use the following solution:

    from selenium import webdriver
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.support import expected_conditions as EC
    
    options = webdriver.ChromeOptions() 
    options.add_argument("start-maximized")
    options.add_argument('disable-infobars')
    driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
    driver.get("https://www.audiobooks.com/signup")
    WebDriverWait(driver, 20).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[@id='braintree-hosted-field-number']")))
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='number' and @id='credit-card-number']"))).send_keys("1234567890987654")
    
  • Browser Snapshot

The error message

Unable to locate the element

usually because element generated by javascript, use WebDriverWait

inputCC = WebDriverWait(driver, 15).until(
    lambda driver: driver.find_element_by_xpath("//input[@id='credit-card-number']")
)
inputCC.send_keys(creditcardnumber)

other possibility is the element located in iframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!