Python: Log into the website using selenium? - python-3.x

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?

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()

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.

Related

Session expired error immediately after logging in using Selenium Chrome WebDriver in Python

I am trying to login to the webpage below and my code is able to do that for me with no errors. But the moment the code hits the "Login" button, the following error message appears:
how do I resolve this? I am using a Chrome Browser with Selenium in python to automate this process. I am getting the same error while using the edge browser as well.
I am using the following code to achieve this:
chrome_driver_path = r"C:\Users\selenium\chromedriver_win32\chromedriver.exe"
options = webdriver.ChromeOptions()
driver = webdriver.Chrome(executable_path=chrome_driver_path,options=options)
wait = WebDriverWait(driver, 5)
#open the webpage and enter the email id on the login page
driver.get("https://auth.slido.com/login? auth_state=eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIsImtpZCI6IkIifQ.eyJqdGkiOiJmNzZjNzNmNC1hODcyLTQ5NzYtOTBiMS1jZWM5NTJiNTRmNmYiLCJjbHVzdGVyX2lkIjoiZXUxIiwiY2xpZW50SWQiOiI2OTFmNGMxNi1kZTM1LTExZWItYmE4MC0wMjQyYWMxMzAwMDQiLCJwcm9tcHQiOiJjb25zZW50IiwicmVkaXJlY3RVcmkiOiJodHRwczovL2FkbWluLnNsaS5kby9vYXV0aC1jYWxsYmFjayIsInNjb3BlIjoib3BlbmlkIG9mZmxpbmVfYWNjZXNzIGVtYWlsIHByb2ZpbGUgc3lzdGVtOnNjb3BlOmFjcXVpcmUiLCJzZXNzaW9uIjp7ImlzQXV0aE4iOmZhbHNlLCJpc0ludGVybmFsIjp0cnVlfSwic3RhdGUiOiJleUp5WldScGNtVmpkQ0k2SW1GSVVqQmpTRTAyVEhrNWFGcEhNWEJpYVRWNllrZHJkVnBIT0haYVdGcHNZbTVTZWlKOSIsImNsYWltcyI6e30sImlhdCI6MTY3NTE3NTk3MSwiZXhwIjoxNjc1MTc3NzcxLCJpc3MiOiJodHRwczovL2F1dGguc2xpZG8uY29tIn0.Y7WjeXgVCoZMKTfbJ2iy-O7UQn8_-vz_ZhgwsyuyrxervrMnBRYN-oCGEnRI5SnQDlRxif1qBSpDUxNSOOGnv4wgCTLRfq-X-AE7dHeOFcvAdGmmCl_HGGEpQ5plA7yT7f7pMt6xibK2ZhwNL7KwY_OUgVpgz0ROcSv7wBQVNGcx2Am0NfGZw1eDEhBfyiUTktGqTDVyQfmNAB3F7G7PtvgoXEikLLTZBqBY93d5w4RrjxIt6tzdiNpBKwwMulXmSzr0OGnLiRCQXsNfLyst028Hmoq_HsxFLICTKxZTOkelFB83XM1zGRnUkjCOCT-EhQbT0I-hJvQccdlbCdjXKg")
#first accepting all cookies before moving to our actual search
driver.find_element(By.XPATH,"/html/body/div[2]/div[2]/button[2]").click()
time.sleep(2)
#assigining the username
driver.find_element(By.ID,"email").send_keys("xyz#abc.com")
#then clicking on the continue button and move forward
driver.find_element(By.XPATH, "/html/body/auth-root/auth-layout/div[3]/div/auth-login/div/div/form/auth-recaptcha/div/div[2]/button").click()
time.sleep(2)
#now we need to assing the password to login
driver.find_element(By.ID,"password").send_keys("####")
#click on the login button next
driver.find_element(By.XPATH, "/html/body/auth-root/auth-layout/div[3]/div/auth- login/div/div/form/auth-recaptcha/div/div[2]/button").click()
time.sleep(2)
print("Logged in")

Google consent automatic accepting

I am new newbie to Selenium Driver. I am trying to click 'Agree' button automatically on google search pop up using selenium driver interface. I am using python and the code is:
driver.get(f"https://www.google.com")
driver.find_element_by_xpath('//*[#id="introAgreeButton"]').click()
But this is not working. Can someone help me?google auto pop up for consent
this worked for me:
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars') #attempting to disable infobar
browser=webdriver.Chrome(options=options,
executable_path=r'/usr/local/bin/chromedriver')
browser.get('http://www.google.com')
browser.switch_to.frame(browser.find_element_by_xpath("//iframe[contains(#src,
'consent.google.com')]"))
time.sleep(5)
browser.find_element_by_xpath('//*[#id="introAgreeButton"]/span/span').click()
browser.find_elements(By.XPATH, "//form //div[#role = 'button' and #id =
'introAgreeButton'").click()

Python Selenium : How to stop Auto Refreshing of Internal Elements of a Webpage in Selenium?

I want to automate mail checking on temp-mail.org
But the problem is the page keeps on refreshing internal components of page and therefore Selenium can't target mail.
My code:
mail= browser.find_element_by_xpath('//tbody/tr/td[#title="__mail title__"]')
mail.click()
But there is no response.
Then I noticed that contents of page are auto refreshing internally.
So how to stop this auto refreshing?
How about stopping the script aftre 3/5 secs of its runtime.
Try this :
fire = webdriver.FirefoxProfile()
fire.set_preference("http.response.timeout", 3)
fire.set_preference("dom.max_script_run_time", 3)
driver = webdriver.Firefox(firefox_profile=fire)
driver.get("url")

trouble getting the current url on selenium

I want to get the current url when I am running Selenium.
I looked at this stackoverflow page: How do I get current URL in Selenium Webdriver 2 Python?
and tried the things posted but it's not working. I am attaching my code below:
from selenium import webdriver
#launch firefox
driver = webdriver.Firefox()
url1='https://poshmark.com/search?'
# search in a window a window
driver.get(url1)
xpath='//input[#id="user-search-box"]'
searchBox=driver.find_element_by_xpath(xpath)
brand="freepeople"
style="top"
searchBox.send_keys(' '.join([brand,"sequin",style]))
from selenium.webdriver.common.keys import Keys
#EQUIValent of hitting enter key
searchBox.send_keys(Keys.ENTER)
print(driver.current_url)
my code prints https://poshmark.com/search? but it should print: https://poshmark.com/search?query=freepeople+sequin+top&type=listings&department=Women because that is what selenium goes to.
The issue is that there is no lag between your searchBox.send_keys(Keys.ENTER) and print(driver.current_url).
There should be some time lag, so that the statement can pick the url change. If your code fires before url has actually changed, it gives you old url only.
The workaround would be to add time.sleep(1) to wait for 1 second. A hard coded sleep is not a good option though. You should do one of the following
Keep polling url and wait for the change to happen or the url
Wait for a object that you know would appear when the new page comes
Instead of using Keys.Enter simulate the operation using a .click() on search button if it is available
Usually when you use click method in selenium it takes cared of the page changes, so you don't see such issues. Here you press a key using selenium, which doesn't do any kind of waiting for page load. That is why you see the issue in the first place
I had the same issue and I came up with solution that uses default explicit wait (see how explicit wait works in documentation).
Here is my solution
class UrlHasChanged:
def __init__(self, old_url):
self.old_url = old_url
def __call__(self, driver):
return driver.current_url != self.old_url:
#contextmanager
def url_change(driver):
current_url = driver.current_url
yield
WebDriverWait(driver, 10).until(UrlHasChanged(current_url))
Explanation:
At first, I created my own wait condition (see here) that takes old_url as a parameter (url from before action was made) and checks whether old url is the same like current_url after some action. It returns false when both urls are the same and true otherwise.
Then, I created context manager to wrap action that I wanted to make, and I saved url before action was made, and after that I used WebDriverWait with created before wait condition.
Thanks to that solution I can now reuse this function with any action that changes url to wait for the change like that:
with url_change(driver):
login_panel.login_user(normal_user['username'], new_password)
assert driver.current_url == dashboard.url
It is safe because WebDriverWait(driver, 10).until(UrlHasChanged(current_url)) waits until current url will change and after 10 seconds it will stop waiting by throwing an exception.
What do you think about this?
I fixed this problem by clicking on the button by using href. Then do driver.get(hreflink). Click() was not working for me!

Chrome alert pop up not detected in Selenium

I am having issues with handling Authentication pop up in Chrome via Selenium.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('URL')
time.sleep(5)
alert = driver.switch_to_alert()
alert.send_keys('Username')
alert.send_keys(Keys.TAB)
alert.send_keys('Password')
This returns an error--
"selenium.common.exceptions.NoAlertPresentException: Message: no alert open"
Alternatively, I also tried the following code:
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get('https://Username:Password#URL')
The second code works partially-
In Chrome the user is logged in but the page does not load. Only a blank page is displayed. Once the blank page is loaded, i passed only the URL(without user credentials) and it works fine.
In Firefox, the webpage loads perfectly.
Basically, the issue is with Chrome.
Any help is appreciated.
Thanks!
The second code you have tried is correct with a slight mistake.
public void loginToSystem(String username,String password, String url){
driver = webdriver.Chrome()
driver.get("https://"+username+":"+password+"# "+URL);
}
First try to wait for alert present.
wait = WebDriverWait(driver, 10)
wait.until(EC.alert_is_present())
if alert is present then only move forword to next step.
And also can you please check the screenshot if alert is actually present when testcase is failing.

Resources