0

i'm using selenium with python to register into a website. My problem is that when i need to insert the credit card number and the security number, though i copy the xpath of the field, wait for the field to be visible and clickable, python raise a timeout exception becouse he didn't find any field with that xpath. The html code of the field is that:

<input id="cardnumber" autocomplete="cc-number" type="tel" pattern="[0-9]*" placeholder="1111 2222 3333 4444" data-encrypted-name="number" class="form-control">

UPDATE: Maybe I understand. When I print out driver.page_source I notice that it's not loaded properly, and now I understand why Selenium can't find nothing, neither by xpath, neither by name or everything. I notice that what is loaded can be clicked and all, but some fields are not loaded. So why Selenium behave like that?

UPDATE AGAIN: Solved, the solution is that i need to switch iframe every time.

Gianla
  • 63
  • 8

2 Answers2

0

Try This By Adding Some Wait:

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



driver = webdriver.Chrome()
driver.maximize_window()
driver.get("Your Website")
try:
    cardnumber = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "#cardnumber")))
except:
    print('Sorry!')


cardnumber.send_keys('XXXXX')
Hamza Lachi
  • 1,046
  • 7
  • 25
0

You are passing wrong ID. You dont need to pass #when you are trying to identify element with its ID.

cardnumber = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.ID, "cardnumber")))

Please check locator strategies :https://selenium-python.readthedocs.io/locating-elements.html

SeleniumUser
  • 4,065
  • 2
  • 7
  • 30