Google consent automatic accepting - python-3.x

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

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

Chromdriver UserData configuration with Selenium

I have this simple function with Selenium
def config():
path = r'C:\Users\George\Desktop\Bot\User Data'
options = webdriver.ChromeOptions()
options.add_argument('user-data-dir=' + path)
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'C:\Users\George\Desktop\Bot\chromedriver.exe', chrome_options=options)
return driver
Which is working fine and as expected, But am making a simple bot to do tasks over a website that require sessions and cookies and so on.
So what I have to make each now and then, Is to copy the real browser User Data from:
C:\Users\George\AppData\Local\Google\Chrome\User Data
to my random location
'C:\Users\George\Desktop\Bot\User Data'
And it works fine, The reason am doing this is that I want to avoid login, and have the browser that I work with independent with the bot driver browser, And I Usually keep it laid down and never open while its doing my work.
Question
Is there a way, That I can automatically get the current session instant of copying Files (like get one open tab with all information attached )?
Is there a better way to do this? ( am sure there are plenty of better ways)
Thanks for the help, Any suggestion, Links, Blogs are much appriciated.
You won't be required to copy the real browser User Data from:
C:\Users\George\AppData\Local\Google\Chrome\User Data
to:
C:\Users\George\Desktop\Bot\User Data
each now and then if you point user-data-dir to the real google-chrome User Data directory as follows:
def config():
options = webdriver.ChromeOptions()
# next line you need to replace the random location with actual browser data location
options.add_argument(r"--user-data-dir=C:\Users\George\AppData\Local\Google\Chrome\User Data")
options.add_experimental_option("excludeSwitches", ["enable-automation"])
options.add_experimental_option('useAutomationExtension', False)
options.add_argument('--disable-blink-features=AutomationControlled')
driver = webdriver.Chrome(executable_path=r'C:\Users\George\Desktop\Bot\chromedriver.exe', chrome_options=options)
return driver
References
You can find a couple of relevant detailed discussion in:
Selenium: Point towards default Chrome session

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?
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.

Handling window pop up with selenium [duplicate]

This question already has answers here:
How to allow or deny notification geo-location microphone camera pop up
(2 answers)
Closed 3 years ago.
I am currently working on a bot that logs into instagram, I currently have the script to log in and turn on notifications but when then I get a window pop the code does not click on allow. I have been stuck for quite some time. Thank you in advance for your help.
def allow_noti():
allow_noti = WebDriverWait(browser, 20).until(EC.element_to_be_clickable((By.XPATH,
"//button[contains(.,'Turn On')]")))
allow_noti.click()
allow_browser = WebDriverWait(browser, 10).until(EC.element_to_be_clickable((By.XPATH, '//*
[text()="Allow"]'))).click()
allow_browser.click()
Hope this helps
public static void main(String[] args)
{
ChromeOptions options = new ChromeOptions();
options.addArguments("--disable-notifications");
System.setProperty("webdriver.chrome.driver", "/home/users/ss/bb/chromedriver");
WebDriver driver =new ChromeDriver(options);
driver.get("http://google.com");
driver.manage().window().maximize();
}

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