Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to login to a website using selenium in Python?

I am trying to login to this website using my credentials. However, although I'm providing what they have in their html, it's not working

Currently, I have

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

CHROMEDRIVER_PATH = './chromedriver'

chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("start-maximized")
chrome_options.add_argument("--disable-blink-features")
chrome_options.add_argument("--disable-blink-features=AutomationControlled")

LOGIN_PAGE = "https://www.seekingalpha.com/login"
ACCOUNT = "account.com"
PASSWORD = "password"

driver = webdriver.Chrome(executable_path=CHROMEDRIVER_PATH, chrome_options=chrome_options)
driver.execute_script("Object.defineProperty(navigator, 'webdriver', {get: () => undefined})")
driver.execute_cdp_cmd('Network.setUserAgentOverride', {"userAgent": 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.53 Safari/537.36'})

driver.get("https://www.seekingalpha.com/login")
username = driver.find_element_by_name("email")
username.send_keys(ACCOUNT)
password = driver.find_element_by_name("password")
password.send_keys(PASSWORD)

submit_button = driver.find_element_by_css_selector("button._60b46-2mbi1 _60b46-2LbhQ _60b46-1HfKK _60b46-qZydf _60b46-1uOHx _60b46-2NDcV _60b46-3YvwX _60b46-22lGb _60b46-1qE-_ _60b46-3DOuR _60b46-1UPRS _12b23-2JWwg _60b46-EQJB_")
submit_button.click()


driver.get("https://seekingalpha.com/article/4414043-agenus-inc-agen-ceo-garo-armen-on-q4-2020-results-earnings-call-transcript")
text_element = driver.find_elements_by_xpath('//*')

text = text_element

for t in text:
    print(t.text)

and I keep getting

selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"[name="email"]"}
  (Session info: headless chrome=90.0.4430.212)
like image 290
DSMK Swab Avatar asked Jul 20 '26 09:07

DSMK Swab


1 Answers

This problem can be solved with the help of explicit wait. See the code below :

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


driver = webdriver.Chrome("C:\\Users\\etc\\Desktop\\Selenium+Python\\chromedriver.exe", options=options)
wait = WebDriverWait(driver, 30)
driver.get("https://www.seekingalpha.com/login")
wait.until(EC.element_to_be_clickable((By.NAME, "email"))).send_keys("some user name")
wait.until(EC.element_to_be_clickable((By.ID, "signInPasswordField"))).send_keys("your password")
wait.until(EC.element_to_be_clickable((By.XPATH, "//button[text()='Sign in']"))).click()
print("Logged in")

Read more about explicit wait here

like image 188
cruisepandey Avatar answered Jul 22 '26 22:07

cruisepandey