Home > Back-end >  Unable to find input element with Selenium and Python
Unable to find input element with Selenium and Python

Time:07-29

I am new in selenium and python. I trying to find element using selenium but no matter what I tried (xpath, CSS) I always got the following message:

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element

Here the code I wrote :

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
from time import sleep
from requests_html import HTML

options = Options()
# options.add_argument("--headless")
options.add_argument('disable-notifications')
options.add_argument('--disable-infobars')
options.add_argument('--disable-blink-features=AutomationControlled')

driver = webdriver.Chrome(executable_path=r'C:/Users/XXX/PycharmProjects/chromedriver.exe', options=options)

first_url = "https://www.micromania.fr/jeu-concours-summer-show.html" 

driver.get(first_url)

# For the following, it works
refuse = driver.find_element_by_xpath('//*[@id="truste-consent-required"]').click()
time.sleep(2)

But when I tried to do a driver.find_element_by -CSS, Xpath, to find for the mail field to be able to fill it, I met the error message.

HTML of the mail field I tried to reach is there:

<input data-v-0f8f86ae="" type="email" placeholder="Email*" >

UPDATE enter image description here

CodePudding user response:

You need to first click on the account icon, having class sidebar-login header-link-item icon-account no-decoration color-white. That click will do a XHR network call, which will bring some new elements into page, including email address field. Also, it's wise to wait for elements to load in page before searching them, instead of just searching for them. Also, it's wise to wrap the cookie button dismissal in a try/except block, just in case it's not popping up all the time.

Adapting the following snippet to your code will get you what you're after:

url='https://www.micromania.fr/jeu-concours-summer-show.html'
browser.get(url)
WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".sidebar-login.header-link-item.icon-account.no-decoration.color-white"))).click()
email_field = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@title='Adresse email']")))
email_field.click()
email_field.send_keys('my amazing email address')
print('done')

EDIT: for the email field I'm not able to see (restricted to French IPs only):

email_2_field = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//div[@class='home']/input[@type='email']")))
email_2_field.click()

CodePudding user response:

Given the HTML:

<input data-v-0f8f86ae="" type="email" placeholder="Email*" >

As the desired element is a dynamic element, to send a character sequence within the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.input[type='email'][placeholder^='Email']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@class='input' and starts-with(@placeholder, 'Email')][@type='email']"))).click()
    
  • Note: You have to add the following imports :

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