3

Using Selenium, I am unable to locate the "email" element on the Udemy website.

Here's what I tried:

browser.get('https://www.udemy.com/join/login-popup/')
browser.implicitly_wait(5)

email = browser.find_element(By.ID, 'email--1')
print(email)

but it gives NoSuchElementException while "email" element isn't even in an iframe, as far as I know.

So, how can I locate this specific element?

Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
shasherazi
  • 100
  • 10

1 Answers1

5

In this case, you're probably looking for the element before the page loads.

You should use the WebDriverWait class of Selenium and the condition presence_of_element_located of the expected_conditions, as shown in the example below:

from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


browser.get('https://www.udemy.com/join/login-popup/')
timeout = 20  # Number of seconds before timing out.
email = WebDriverWait(browser, timeout).until(EC.presence_of_element_located((By.ID, 'email--1')))
email.send_keys("example@email.com")

In the above snippet, Selenium WebDriver waits up to 20 seconds before throwing a TimeoutException, unless it finds the element to return within the above time. WebDriverWait, by default, calls the ExpectedCondition every 500 milliseconds until it returns successfully.

Finally, presence_of_element_located is an expectation for determining whether an element is present on a page's DOM, without necessarily implying that the element is visible. When the element is found, the locator returns the WebElement.

Andreas Violaris
  • 2,465
  • 5
  • 13
  • 26
  • it gives the `TimeoutException`, can you get it to run on your machine? – shasherazi Jun 05 '22 at 10:17
  • The code was tested before it was posted. Does the program load the page normally when you run it? – Andreas Violaris Jun 05 '22 at 10:27
  • Don't think so. Can't even access the udemy logo on the main site – shasherazi Jun 06 '22 at 12:44
  • Paste the full code you use at pastebin.com and provide the link here. – Andreas Violaris Jun 06 '22 at 13:09
  • [here](https://www.toptal.com/developers/hastebin/anohehonav.python) – shasherazi Jun 06 '22 at 14:21
  • 1
    In line 8, you use a deprecated method that requires you to enter the path of the chrome driver, when you actually give it a string with the value: "chromedriver". Use the webdriver_manager package instead, as shown in [this](https://pastebin.com/xMzyviqQ) snippet and don't forget to mark the answer as "correct" by clicking on the check mark, on the left side of the answer, to toggle it from greyed out to filled in, so that anyone else who encounters the same issue knows that there is a confirmed solution. – Andreas Violaris Jun 06 '22 at 17:21
  • 1
    turns out that Cloudflare was being suspicious and wanted a human verification. saw that by taking a screenshot of the page. thanks to you too, manually downloading the driver is a headache – shasherazi Jun 07 '22 at 19:20