mohammed awni
mohammed awni

Reputation: 168

How to get HTML elements from dynamically generated page in Selenium?

I'm trying to sign in my account in Figure eight

using selenium and python but i can't because there is no any HTML elements in the page only these lines exist.

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8">
    <meta content="ie=edge" http-equiv="x-ua-compatible">
    <title>Figure Eight Contributor Portal</title>
    <link href="/favicon.ico" rel="shortcut icon" />
    <link href="//fonts.googleapis.com/css?family=Lato:100,300,400,700,900" rel="stylesheet" />
    <link href="https://use.fontawesome.com/releases/v5.2.0/css/all.css" rel="stylesheet" />
  </head>
  <body>
    <div id="app">
    </div>
    <script src="/1.index.js?eca43d17e540e40a3231" type="text/javascript"></script>
    <script src="/index.js?eca43d17e540e40a3231" type="text/javascript"></script>
  </body>
</html>

Surprisingly when i use inspect element i can get the HTML elements. Searching for similar topic on stackoverflow and google but can't find any solution. My code trying to sign in is :

from selenium import webdriver
from selenium.webdriver.support.expected_conditions import presence_of_element_located
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import Chrome

def sign_in(driver, url):
    driver.get(url)
    WebDriverWait(driver, 60).until(presence_of_element_located(driver.find_element_by_id('Email')))
    username = driver.find_element_by_id("Email")
    password = driver.find_element_by_id("Password")
    username.send_keys("##########")
    password.send_keys("@@@@@@@@@@")
    driver.find_element_by_name("Sign in").click()
    return True, "Success"

url = "https://contributors.figure-eight.work/login"
driver = Chrome()
result, message = sign_in(driver, url)
if result:
    print(message)

Upvotes: 0

Views: 1856

Answers (2)

undetected Selenium
undetected Selenium

Reputation: 193088

To send a character sequence to the Email and Password field you have to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    driver.get('https://contributors.figure-eight.work/login')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[name='email']"))).send_keys("[email protected]")
    driver.find_element_by_css_selector("input[name='password']").send_keys("[email protected]")
    driver.find_element_by_css_selector("div.b-Login__ButtonHolder>a").click()
    
  • Using XPATH:

    driver.get('https://contributors.figure-eight.work/login')
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='email']"))).send_keys("[email protected]")
    driver.find_element_by_xpath("//input[@name='password']").send_keys("[email protected]")
    driver.find_element_by_xpath("//div[@class='b-Login__ButtonHolder']/a").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
    
  • Browser Snapshot:

future-eight

Upvotes: 2

Sameer Arora
Sameer Arora

Reputation: 4507

All the elements are present on the page and you can find those by inspecting any element on the page on chrome or firefox.

You can sign in using the code(Have added the xpaths for all the elements in the code below):

from selenium import webdriver
from selenium.webdriver.support.expected_conditions import 
presence_of_element_located
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver import Chrome



def sign_in(driver, url):
    driver.get(url)
    WebDriverWait(driver, 60).until(
    presence_of_element_located(
        driver.find_element_by_xpath("//input[@name='email']")))
    username = driver.find_element_by_xpath("//input[@name='email']")
    password = driver.find_element_by_xpath("//input[@name='password']")
    username.send_keys("##########")
    password.send_keys("@@@@@@@@@@")
    driver.find_element_by_xpath("//a[text()='Sign in']").click()
    return True, "Success"

url = "https://contributors.figure-eight.work/login"
driver = Chrome()
result, message = sign_in(driver, url)
if result:
    print(message)

Upvotes: 1

Related Questions