Home > Net >  How to login within Tradingview site using Selenium and Python
How to login within Tradingview site using Selenium and Python

Time:02-24

I'm new to Python. To enter the Tradingview.com site with Selenium library.

I wrote the following code and used Xpath and CSS selector to give the address, and the Click method, but it does not work properly. Has anyone solved this problem?

import time
from selenium import webdriver
from webdriver_manager.firefox import GeckoDriverManager

driver = webdriver.Firefox(executable_path=GeckoDriverManager().install())

longInfo = ["xxx.gmail.com", "11"]

try:
    driver.get("https://www.tradingview.com/#signin")
    driver.set_page_load_timeout(20)
    driver.maximize_window()

# click email Button for logging page
    driver.find_element_by_xpath("/html/body/div[7]/div/div[2]/div/div/div/div/div/div/div[1]/div[4]/div/span").click()
    time.sleep(5)
    driver.find_element_by_css_selector(
    "#email-signin__user-name-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02").send_keys(longInfo[0])
    driver.find_element_by_css_selector(
    "#email-signin__password-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02").send_keys(longInfo[1])

# click sign in Button
    driver.find_element_by_xpath(
    "/html/body/div[7]/div/div[2]/div/div/div/div/div/div/form/div[5]/div[2]/button/span[2]").click()

    input("type for exit")

    driver.quit()
except Exception as e:
    print(e)
    driver.quit()

CodePudding user response:

It seems the locator you have used its dynamic, you need to identify the element with better approach.

Its required synchronization time while navigating.

Use explicit wait and wait for element to be clickable.

longInfo = ["xxx.gmail.com", "11"]
driver.get("https://www.tradingview.com/#signin")
wait=WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Email']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='username']"))).send_keys(longInfo[0])
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[name='password']"))).send_keys(longInfo[1])
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[.//span[contains(., 'Sign in')]]"))).click()

Use following libraries.

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

CodePudding user response:

These values of the id attribute:

  • email-signin__user-name-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02
  • email-signin__password-input__e07a4b49-2f94-4b3e-a3f8-934a5744fe02

are dynamically generated and would change everytime you access the webpage afresh. Instead you need to use locator strategies based on static attributes.


Solution

To login within Trading View website you need to induce WebDriverWait for the element_to_be_clickable() and you can use the following locator strategies:

driver.get("https://www.tradingview.com/#signin")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[text()='Email']"))).click()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='username']"))).send_keys("[email protected]")
driver.find_element(By.XPATH, "//input[@name='password']").send_keys("matin_mhz"   Keys.RETURN)

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