Search code examples
seleniumselenium-webdriverxpathcss-selectorswebdriverwait

Selenium cant find element


<input class="form__input number-verify" type="tel" id="sms-number" maxlength="20">

tel = browser.find_element(By.ID , "form__input number-verify")

It's not working. I want to click that element and then write (ı know how do it) but ı cant solve my problem.

and that all code.

from selenium import webdriver
from bs4 import BeautifulSoup
from selenium.webdriver.common.by import By
import time
browser = webdriver.Edge()
browser.get("https://globfone.com/send-text/")
soup = BeautifulSoup(browser.page_source, 'html.parser')
giris_yap = browser.find_element(By.NAME,"sender-name")
giris_yap.click()
giris_yap.send_keys("abc")
next_button = browser.find_element(By.CLASS_NAME, "cover__btn")
next_button.click()
time.sleep(5)
tel = browser.find_element(By.ID , "form__input number-verify")

time.sleep(5)
browser.close()

Solution

  • Your main mistake is that form__input number-verify is not ID, these are 2 class values, so you can use CSS Selector or Xpath to use these 2 class names.
    Also, you should not use hardcoded pauses. WebDriverWait expected_conditions should be used instead.
    The following code works:

    from selenium import webdriver
    from selenium.webdriver.chrome.service import Service
    from selenium.webdriver.chrome.options import Options
    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
    options = Options()
    options.add_argument("start-maximized")
    
    webdriver_service = Service('C:\webdrivers\chromedriver.exe')
    driver = webdriver.Chrome(service=webdriver_service, options=options)
    
    url = 'https://globfone.com/send-text/'
    driver.get(url)
    wait = WebDriverWait(driver, 20)
    
    giris_yap = wait.until(EC.element_to_be_clickable((By.NAME, "sender-name")))
    giris_yap.click()
    giris_yap.send_keys("abc")
    wait.until(EC.element_to_be_clickable((By.CLASS_NAME, "cover__btn"))).click()
    tel = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".form__input.number-verify")))
    tel.send_keys("755")
    

    the result is:

    enter image description here