1

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*" class="input">

UPDATE enter image description here

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352

2 Answers2

0

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()
Barry the Platipus
  • 9,594
  • 2
  • 6
  • 30
  • Thank you for your reply. My apologizes I see I was not accurate in my description. I want to have access to the email not in the account icon but the other one : I am not able to reach it unfortunately – ThankYouForHelping Jul 28 '22 at 12:37
  • When I visit the page, I see a `403 Forbidden`, probably the website is for French IPs only, so I don't see that email field. Nonetheless: try locating that element with `WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@data-v-0f8f86ae='']")))`, click on it, then send the text you want to it. If you still cannot locate it, there are a few other ways as well, just let me know. – Barry the Platipus Jul 28 '22 at 13:06
  • Thank you again but it did not work T_T. I cannot imagine how complicate it can be wihout seeing the page so thank you for your proposal. I also notice that the email field is within a "flex" structure. I am able to interact with element that. – ThankYouForHelping Jul 28 '22 at 15:36
  • While usually a bad practice, can you edit your question to include an image of the page, with dev tools open, with that actual element? – Barry the Platipus Jul 28 '22 at 15:43
  • I update the question with the image. I used Chrome inspect tool. – ThankYouForHelping Jul 28 '22 at 16:11
  • And I updated my reponse. If that won't work either, then you should select all inputs with type=email, count them, and try one by one. – Barry the Platipus Jul 28 '22 at 17:17
0

Given the HTML:

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

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
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352