How can I log in to Yahoo FInance using Python Selenium? - python

I've tried the following way, but every time I click on the "Next" button in the login, chromdriver would open a new tab and redirect me to this page.
from selenium import webdriver
path = "https://login.yahoo.com/config/login?.src=finance&.intl=us&.lang=en-US&.done=https%3A%2F%2Ffinance.yahoo.com%2Fquotes%2Flogin%2Fview%2Fv1%2F"
option = webdriver.Chrome()
option.add_argument("--incognito")
option.add_argument("--disable-notifications")
browser = webdriver.Chrome("/path/to/chromedriver", optiont=option)
browser.get(path)
browser.find_element_by_name("username").send_keys("username")
# all three attempts below redirected me to the page mentioned above
browser.find_element_by_name("signin").click()
browser.find_element_by_class_name("button-container").click()
browser.find_element_by_id("login-username-form").click()
I wonder this is some kind of secutiy they have considering the redirected page.
I also tried sending password in the hidden-input-container
browser.find_element_by_name("passwd").send_keys("password")
to get selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable error. I guess I need to press the Next button before sending the password.
I'd much appreciate any help on this issue.

Induce WebDriverWait() and wait for element_to_be_clickable()
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
from selenium.webdriver.chrome.options import Options
path = "https://login.yahoo.com/config/login?.src=finance&.intl=us&.lang=en-US&.done=https%3A%2F%2Ffinance.yahoo.com%2Fquotes%2Flogin%2Fview%2Fv1%2F"
option = Options()
option.add_argument("--incognito")
option.add_argument("--disable-notifications")
browser = webdriver.Chrome(executable_path="/path/to/chromedriver",options=option)
browser.get(path)
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"username"))).send_keys("validusername")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"signin"))).click()
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.NAME,"password"))).send_keys("password")
WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.ID,"login-signin"))).click()
Browser snapshot:

Related

Trying to log into website with selenium throws either TimeoutException or ElementNotInteractableException

I am having issues logging into the website (memodo.de/login) since the login form is not interactable. With my code, I get either a timeout exception or a ElementNotinteractable Exception.
Please see here the html code for the website as well as my current code. I would appreciate if someone could help me with the issue.
I already tried using the execute_script command, without any luck.
Thank you!
from selenium import webdriver
from webdriver_manager.chrome import ChromeDriverManager
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
import time
import os
driver = webdriver.Chrome(ChromeDriverManager().install())
driver.get('https://www.memodo.de/login')
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='email']"))).send_keys("email")
WebDriverWait(driver, 5).until(EC.element_to_be_clickable((By.XPATH, "//input[#id='passwort']"))).send_keys("password")
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[type='submit']"))).click()
HTML Code for the site
The xpaths you are trying are highlighting many Elements in the DOM. The email and password are highlighting 2 Elements each and the submit button is highlighting 5 Elements in the DOM.
The locators we use to find the Elements should be unique that is 1/1 which is visible next to the xpath in the DOM. Link to Refer Go through the How do I write good locators for more clarity.
You can try like below.
driver.get("https://www.memodo.de/login")
wait = WebDriverWait(driver,30)
email = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='register--login-email']/input")))
email.send_keys("Email#email.com")
password = wait.until(EC.element_to_be_clickable((By.XPATH,"//div[#class='register--login-password']/input")))
password.send_keys("password")
submit = wait.until(EC.element_to_be_clickable((By.XPATH,"//form[#id='login--form']//button")))
submit.click()
You can use these xpaths too.
(//input[#id='email'])[2] # For Email Field
(//input[#id='passwort'])[2] # For password Field
//form[#id='login--form']//button # For submit button

Python 2.7 Selenium No Such Element on Website

I'm trying to do some webscraping from a betting website:
As part of the process, I have to click on the different buttons under the "Favourites" section on the left side to select different competitions.
Let's take the ENG Premier League button as example. I identified the button as:
(source: 666kb.com)
The XPath is: //*[#id="SportMenuF"]/div[3] and the ID is 91.
My code for clicking on the button is as follows:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL Removed")
content = driver.find_element_by_xpath('//*[#id="SportMenuF"]/div[3]')
content.click()
Unfortunately, I always get this error message when I run the script:
"no such element: Unable to locate element:
{"method":"xpath","selector":"//*[#id="SportMenuF"]/div[3]"}"
I have tried different identifiers such as CCS Selector, ID and, as shown in the example above, the Xpath. I tried using waits and explicit conditions, too. None of this has worked.
I also attempted scraping some values from the website without any success:
from selenium import webdriver
from selenium.webdriver.common.by import By
chrome_path = "C:\Python27\Scripts\chromedriver_win32\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("URL removed")
content = driver.find_elements_by_class_name('price-val')
for entry in content:
print entry.text
Same problem, nothing shows up.
The website embeddes an iframe from a different website. Could this be the cause of my problems? I tried scraping directly from the iframe URL, too, which didn't work, either.
I would appreciate any suggestions.
Sometimes elements are either hiding behind an iframe, or they haven't loaded yet
For the iframe check, try:
driver.switch_to.frame(0)
For the wait check, try:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.XPATH, '-put the x-path here-')))

Click ''Not now" on Instagram notifications ask using Selenium and Python

I've written a script that successfully makes the login on Instagram.
When I should go on my account, at home, the website displays a popup that asks you if you want notifications.
At this point, I tried a lot of solutions, but I got nothing.
I just want that, when the pop-up is displayed, the script should click on "Not now".
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
ids = []
driver = webdriver.Chrome(executable_path = '/usr/local/bin/chromedriver')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
usm = driver.find_element_by_name('username').send_keys("**")
pwd = driver.find_element_by_name('password').send_keys("**")
btnLog = driver.find_element_by_tag_name('form').submit()
acpt = driver.find_element_by_xpath("//*[contains(#class, 'aOOlW HoLwm ')]")
In the image, there's the line of the button highlighted that I want to click:
Try the following code for this:
from selenium.webdriver.support import ui
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
ui.WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".aOOlW.HoLwm"))).click()
PS: I have used 10-second wait for the element to be clickable before click on it.
Hope it helps you!
To click() on the element with text as Not now on Instagram popup notification you can use the following solution:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
driver = webdriver.Chrome(chrome_options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://www.instagram.com/accounts/login/?source=auth_switcher")
driver.find_element_by_name('username').send_keys("Giacomo")
driver.find_element_by_name('password').send_keys("Maraglino")
driver.find_element_by_tag_name('form').submit()
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[contains(.,'Non ora')]"))).click()
just add click to acpt, like this:
acpt = driver.find_element_by_xpath("//*[contains(#class, 'aOOlW HoLwm ')]")
acpt.click()
For some reason, the solution to this question didn't fully work for me. The script does click on "not now" then I'm redirected to the "Home" page only to find the 'activate notifications' pop-up waiting for me.
This is the solution that I came up with:
Go to https://instagram.com/
Wait for the page to load.
Find the "Not now" button on the button using the full XPATH.
Clicking on it.
Here's the code (inserted after identifying to my Instagram account):
driver.get("https://instagram.com/")
time.sleep(7)
acpt = browser.find_element(by=By.XPATH, value='/html/body/div[1]/div/div/div/div[2]/div/div/div[1]/div/div[2]/div/div/div/div/div/div/div/div[3]/button[2]')
acpt.click()

click() doesn't work in selenium

i am currently using selenium with python and my webdriver is firefox
i tried the click event but it doesn't work
website = www.cloudsightapi.com/api
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.common.exceptions import TimeoutException
import time
from selenium.webdriver.common.action_chains import ActionChains
import os
driver = webdriver.Firefox()
driver.get("http://cloudsightapi.com/api")
wait = WebDriverWait(driver, 10)
element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget")))
element.click()
Please help !!
Clicking via javascript worked for me:
element = wait.until(EC.element_to_be_clickable((By.ID, "dropzoneTarget")))
driver.execute_script("arguments[0].click();", element)
Now, the other problem is that clicking that element would only get you into more troubles. There will a file upload popup being opened. And, the problem is, you cannot control it via selenium.
A common way to approach the problem is to find the file input and set it's value to the absolute path to the file you want to upload, see:
How to upload file ( picture ) with selenium, python
In your case, the input is hidden, make it visible and send the path to it:
element = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "input.dz-hidden-input[type=file]")))
# make the input visible
driver.execute_script("arguments[0].style = {};", element)
element.send_keys("/absolute/path/to/image.jpg")

Unable to submit keys using selenium with python

Following is the code which im trying to run
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
#Create a new firefox session
browser=webdriver.Firefox()
browser.maximize_window()
#navigate to app's homepage
browser.get('http://demo.magentocommerce.com/')
#get searchbox and clear and enter details.
browser.find_element_by_css_selector("a[href='/search']").click()
search=browser.find_element_by_class_name('search-input')
search.click()
time.sleep(5)
search.click()
search.send_keys('phones'+Keys.RETURN)
However, im unable to submit the phones using send_keys.
Am i going wrong somewhere?
Secondly is it possible to always use x-path to locate an element and not rely on id/class/css-selections etc ?
The input element you are interested in has the search_query class name. To make it work without using hardcoded time.sleep() delays, use an Explicit Wait and wait for the search input element to be visible before sending keys to it. Working code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
browser = webdriver.Firefox()
browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get('http://demo.magentocommerce.com/')
browser.find_element_by_css_selector("a[href='/search']").click()
search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-query")))
search.send_keys("phones" + Keys.RETURN)

Categories