Chromedriver to pickup existing Chrome browser session in Python - python

I want to open several weblinks under a website in 1 browser (several tabs). The website requires login and password.
When login and password keyed in. it turns to a verification page, asks for the verification code sent to me by email.
I checked the email and key in verification code on the verification page. Login is successful.
The existing browser is in front of me.
However the codes are not picking it up, and open another tab as wanted. Seems a certain connection is lost.
How can I continue? (or as an alternative, how can Python to reuse the existing Chrome browser?)
The codes usually works well but comes to this case (login, enter verification code), it doesn't.
import os, time
from selenium.webdriver import ChromeOptions, Chrome
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "C:\\Python27\\Scripts\\chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
opts = ChromeOptions() # leave browser open after code
opts.add_experimental_option("detach", True) # leave browser open after code
opts.add_argument('disable-infobars')
driver = webdriver.Chrome(chromedriver, chrome_options=opts) # leave browser open after code
driver.maximize_window()
verificationErrors = []
accept_next_alert = True
time.sleep(5)
base_url = "https://awebsite.com/"
driver.get(base_url)
window_0 = driver.window_handles[0]
driver.switch_to_window(window_0)
driver.find_element_by_id("username").clear()
driver.find_element_by_id("username").send_keys("username")
driver.find_element_by_id("password").clear()
driver.find_element_by_id("password").send_keys("password")
driver.find_element_by_id("Submit").click()
time.sleep(60)
driver.find_element_by_tag_name('body').send_keys(Keys.CONTROL + 't')
window_1 = driver.window_handles[1]
driver.switch_to_window(window_1)
time.sleep(3)
driver.get('https://anotherwebsite.com')
time.sleep(3)
sys.exit()

You can try below to perform some actions on two different pages/tabs:
# Handle base page
base_url = "https://awebsite.com/"
driver.get(base_url)
window_0 = driver.current_window_handle
...
# Handle new page
driver.execute_script('window.open("https://anotherwebsite.com");')
window_1 = [window for window in driver.window_handles if window != window_0][0]
driver.switch_to_window(window_1)
# driver.close() # To close new tab
...
# Switch back to base page
driver.switch_to_window(window_0)

Related

Trying to sign into google using a selenium python script

I am using selenium to open and sign into google accounts as my first step. I have successfully opened and filled the email response although upon submitting I receive the error of
"This browser or app may not be secure. Learn more Try using a
different browser. If you’re already using a supported browser, you
can refresh your screen and try again to sign in." From google.
Is there any way to get around this? Here is my code below.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
driver.get("https://accounts.google.com/")
print(driver.title)
search = driver.find_element_by_name("identifier")
search.send_keys("email goes here")
search.send_keys(Keys.RETURN)
Was having the same issue and i found this thread in GitHub.
The solution that worked for me was to use this driver: undetected_chromedriver instead of the normal ChromeDriver.
import undetected_chromedriver.v2 as uc
chrome_options = uc.ChromeOptions()
chrome_options.add_argument("--disable-extensions")
chrome_options.add_argument("--disable-popup-blocking")
chrome_options.add_argument("--profile-directory=Default")
chrome_options.add_argument("--disable-plugins-discovery")
chrome_options.add_argument("--incognito")
chrome_options.add_argument("user_agent=DN")
self.browser = uc.Chrome(options=chrome_options)
self.browser.delete_all_cookies()
# example of loggin in to youtube without getting that issue
self.browser.get('http://youtube.com')
login_button_init = self.browser.find_element_by_xpath("//a[#aria-label='Sign in']")
login_button_init.click()
# locate the login button
login_button = self.browser.find_element_by_xpath("//paper-button[#aria-label='Sign in']")
login_button.click()
# get email and set to email input box
email = self.browser.find_element_by_id("identifierId")
myemail = os.environ.get('YOUTUBE_EMAIL')
email.send_keys(myemail)
# click next button
email_next_button = self.browser.find_element_by_id("identifierNext")
email_next_button.click()
# get password and set to password input box
password = self.browser.find_element_by_name("password")
mypassword = os.environ.get('YOUTUBE_PASSWORD')
password.send_keys(mypassword)
sleep(2)
# click next button to log in
pass_next_button = self.browser.find_element_by_id("passwordNext")
pass_next_button.click()

How to keep Firefox Log in status when using Selenium?

I want to keep Firefox Login status when using Selenium WebDriver (geckodriver). I used below python code:
from selenium import webdriver
from selenium.webdriver.firefox.options import Options
import time
try:
fp = webdriver.FirefoxProfile('C:/Users/admin/AppData/Roaming/Mozilla/Firefox/Profiles/mow8ff69.selenium')
options = Options()
options.headless = False
browser = webdriver.Firefox(fp, options=options)
browser.get('https://stackoverflow.com/users/login');
time.sleep(2)
txt_username = browser.find_element_by_id('email')
txt_username.clear()
txt_username.send_keys('***#gmail.com') #replace with your own username
time.sleep(1)
txt_password = browser.find_element_by_id('password')
txt_password.clear()
txt_password.send_keys('*****') #replace with your own password
time.sleep(1)
txt_password.submit()
time.sleep(10)
except Exception as ex:
print('Error: {0}'.format(ex))
finally:
browser.quit()
As shown above, I have been using a User Profile created by "Firefox -p". However, after running above code, when I reopen Firefox using that profile and then go to stackoverflow.com, I'm still not logged in.
So it looks like that Selinum WebDriver actually runs firefox in a "sandbox" which cannot write to the User Profile at all? I know that ChromeDriver has a "--no-sandbox" option but it doesn't seem to be available in geckodriver? Is there any workaround?
I cannot switch to ChromeDriver because there is another bug with it: the "--headless" option actually conflicts with "--user-data-dir" option whereas I really need both.

Python / Selenium - how do i stay signed in after calling a second driver.get()?

I have this code to log into cbt nuggets and afterwards i want to go into my playlists and collect some URLs
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import ui
capabilities = DesiredCapabilities.CHROME
capabilities["goog:loggingPrefs"] = {"performance": "ALL"} # chromedriver 75+
options = webdriver.ChromeOptions()
options.add_argument(f"user-data-dir={userdata_path}") #Path to your chrome profile
# options.add_experimental_option("excludeSwitches", ['enable-automation'])
# options.add_experimental_option("excludeSwitches", ["ignore-certificate-errors", "safebrowsing-disable-download-protection", "safebrowsing-disable-auto-update", "disable-client-side-phishing-detection"])
driver = webdriver.Chrome(executable_path=webdriver_path, options=options)
driver.get("https://www.cbtnuggets.com/login")
logs = driver.get_log("performance")
def page_is_loaded(driver):
return driver.find_element_by_tag_name("body") != None
#wait=ui.WebDriverWait(driver,300)
driver.implicitly_wait(10)
#wait.until(page_is_loaded)
USERNAME = driver.find_element_by_xpath('//*[#id="email"]')
USERNAME.send_keys("johndoe#gmail.com")
PASSWORD = driver.find_element_by_xpath("/html/body/div[1]/div[2]/main/div/div[1]/form/fieldset/div[2]/input")
PASSWORD.send_keys("password")
Login_Button=driver.find_element_by_xpath("/html/body/div[1]/div[2]/main/div/div[1]/form/fieldset/button")
Login_Button.click()
driver.get("https://www.cbtnuggets.com/learn/it-training/playlist/nrn:playlist:user:5fcf88f463ebba00155acb18/2?autostart=1")
it all works as expected, but when the last driver.get() executes, i get thrown back to the login page, but when i manually enter the second URL in the address bar it works as expected without having to log in again.
I dont know if this is a selenium issue, or if i am misunderstanding something about how HTTP Get works.
Have you tried to parse the login result? This might happen because the login request is not fully processed yet.
After Login_Button.click() you should check if the site is logged in successfully or not. You have many ways to check:
If the site redirects: check for the title of the page
If the site display a dialog: create a fluent wait to check for the dialog element to display
If you don't even bother to check, just add time.sleep(5). It's bad practice though.
After the check, now you use driver.get to go to the page you want.

How can I take multiple (a lot) of inputs from a user and repeat code with each input the user gives? (Python)

First, I apologize if I make/made any mistakes, or sound silly. I have only just started learning python about a week ago and I'm trying to build a web scraping program with the little knowledge I currently have. There may be some useless time.sleep functions and things, but I have changed and tested many things to get my script to work, so please ignore those things, unless there are critical errors in my code, or stuff I can improve.
So I have written this script using selenium with intent to take video links from the app "TikTok," and run them through a TikTok video downloader website to download these videos. I have successfully written a script that can be used with one link at a time. For example, the user inputs a link and the script opens the website, pastes the link, submits, and then downloads the video from the link.
I am wondering how I can turn this script into one where the user may input many many links, (up to 100) and repeat the downloading process until there are no more links. I would also like for my program to display the current link in use on the terminal, and say "Successfully Downloaded" every time a video is downloaded. My current script code is below. Any help is greatly appreciated. Thank you.
# imports
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
# set chrome options
options = Options()
options.headless = False
# path of chrome webdriver
PATH= 'C:\Program Files (x86)\chromedriver.exe'
# print text and ask for user link
print('TikTok Auto Download Links BOT')
print('-' * 30)
link_url = input('Enter link to be downloaded: ')
# set browser driver and open window with select size
browser = webdriver.Chrome(PATH, options = options)
browser.set_window_size(1920, 1080)
browser.maximize_window()
browser.get('https://snaptik.app/en-us')
time.sleep(1)
# find url box and paste user's input then submit
url_field = browser.find_element_by_id('url')
url_field.send_keys(link_url)
url_field.submit()
time.sleep(3)
# click download button
download_button = browser.find_element_by_xpath('//*[#id="div_download"]/section/div/div/div/article/div[2]/div/a[1]')
time.sleep(3)
download_button.click()
# print successful for UI
time.sleep(3)
print('Download successful! ')
# wait before closing
time.sleep(3)
# close browser
browser.quit()
from selenium import webdriver
import time
from selenium.webdriver.chrome.options import Options
# set chrome options
options = Options()
options.headless = False
# path of chrome webdriver
PATH= 'C:/Users/COUNT DEXTER/Downloads/chromedriver_win32/chromedriver.exe'
# print text and ask for user link
print('TikTok Auto Download Links BOT')
print('-' * 30)
set_download_limit = 100 #download limit
i=1 #counter
while i<=set_download_limit: #while condition
link_url = input('Enter link to be downloaded: ')
# set browser driver and open window with select size
browser = webdriver.Chrome(PATH, options = options)
browser.set_window_size(1920, 1080)
browser.maximize_window()
browser.get('https://snaptik.app/en-us')
time.sleep(1)
# find url box and paste user's input then submit
url_field = browser.find_element_by_id('url')
url_field.send_keys(link_url)
url_field.submit()
time.sleep(3)
# click download button
download_button = browser.find_element_by_xpath('//*[#id="div_download"]/section/div/div/div/article/div[2]/div/a[1]')
time.sleep(3)
download_button.click()
# print successful for UI
time.sleep(3)
print('Download successful! ')
i+=1 #increment counter, then return to loop
# wait before closing
time.sleep(3)
# close browser
browser.quit()```

Python - Open a link in new Chrome tab with Selenium WebDriver?

I would like to have a URL open in a new tab and closing itself rather than opening in front of the user as it currently stands. I want it to be a discrete as possible.
Here is the python code I have so far that works, but brings the window to the front:
def extract(self, id, pass):
chrome_dir_path = '/Users/<user>/Downloads/chromedriver'
driver = webdriver.Chrome(chrome_dir_path)
driver.implicitly_wait(5)
driver.maximize_window()
driver.get('https://<URL>')
username = driver.find_element_by_id('user')
username.send_keys(USER)
password = driver.find_element_by_id('password')
password.send_keys(PASS)
driver.find_element_by_name('remUID').click()
python_button = driver.find_element_by_class_name('button')
python_button.click()
running headless resolved this issue.

Categories