Muhammad Hassan
Muhammad Hassan

Reputation: 4229

Python: Log into the website using selenium?

I want to login to the following website:

https://www.investing.com/equities/oil---gas-dev-historical-data

Here is what I have tried so far:

from selenium import webdriver
profile = webdriver.FirefoxProfile()
profile.set_preference("browser.download.folderList", 2)
profile.set_preference("browser.download.manager.showWhenStarting", False)
profile.set_preference("browser.download.dir", './ogdc.csv')
profile.set_preference("browser.helperApps.neverAsk.saveToDisk", "application/x-gzip")

driver = webdriver.Firefox(firefox_profile=profile)
driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
driver.find_element_by_class_name("login bold")
driver.find_element_by_id('Email').send_keys('myemail')
driver.find_element_by_id('Password').send_keys('mypass')
driver.find_element_by_partial_link_text("Download Data").click()

But I get following Exception:

NoSuchElementException

How can I login to the above website?

Upvotes: 0

Views: 271

Answers (2)

frianH
frianH

Reputation: 7563

You need click Sign in first to bring up the login popup, try the following code:

driver.get('https://www.investing.com/equities/oil---gas-dev-historical-data')
driver.find_element_by_css_selector("a[class*='login']").click()
driver.find_element_by_id('loginFormUser_email').send_keys('myemail')
driver.find_element_by_id('loginForm_password').send_keys('mypass')
driver.find_element_by_xpath("//div[@id='loginEmailSigning']//following-sibling::a[@class='newButton orange']").click()

Upvotes: 1

Ryuno-Ki
Ryuno-Ki

Reputation: 800

From what I can tell, you need to click the Sign In button („login bold”) in order to open the popup. That one is not part of the initial DOM load and need some time to show up. So basically, wait for #Email and #Password to get visible.

You should be able to use an explicit wait for this.

Upvotes: 0

Related Questions