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.
Related
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()
I'm trying to open instagram.com and then have it redirect to the login screen. However, it opens instagram.com but won't go to the login screen and just sits at the main page.
def login(self):
driver = self.driver
driver.get("https://www.instagram.com/")
time.sleep(2)
login_button = driver.find_element_by_xpath("//a[#href='/accounts/login/?source=auth_switcher/']")
login_button.click()
time.sleep(2)
user_name_elem = driver.find_element_by_xpath("//input[#name='username']")
user_name_elem.clear()
user_name_elem.send_keys(self.username)
password_elem = driver.find_element_by_xpath("//input[#name='password']")
password_elem.clear()
password_elem.send_keys(self.password)
password_elem.send_keys(Keys.ENTER)
sites like Instagram and Facebook filter all terminal access (for security reasons), you can change your terminal browser name to a common one like chrome or firefoxe and try to add delays between actions to make it seem more human
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)
I am working on the below script to open multiple website in new tabs. This is working fine in Firefox as expected.
But it is not working in Chrome. It opens a new tab as the second tab, but the second website link opens in the first tab itself. Later again a new tab is opened as the third tab, then the third link opens in the first tab itself. The commented part ( 5 th line ) is how I call the chromedriver.exe.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
browser = webdriver.Firefox() #Chrome('C:\\data\\books oae\\apex library\\chromedriver') #Firefox()
browser.get('https://trello.com/login')
time.sleep(10)
emailElem = browser.find_element_by_id('user')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('password')
passwordElem.send_keys('test12345')
passwordElem.submit()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
browser.get('https://todoist.com/Users/showLogin')
time.sleep(10)
emailElem = browser.find_element_by_id('email')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('password')
passwordElem.send_keys('test12345')
passwordElem.submit()
body = browser.find_element_by_tag_name("body")
body.send_keys(Keys.CONTROL + 't')
browser.get('https://asana.com/#login')
time.sleep(10)
emailElem = browser.find_element_by_id('login-email-login-modal')
emailElem.send_keys('test123#gmail.com')
passwordElem = browser.find_element_by_id('login-password-login-modal')
passwordElem.send_keys('test12345')
passwordElem.submit()
Splinter is the answer to your problem. It's a wrapper around Selenium that does all your Display-handling for you. Just by creating new Splinter objects, you open new windows. You can specify if you want them to be Chrome or Firefox.
below is part of a code i am using. It works fine except for when firefox opens it only takes up half the screen, and towards the bottom left of the screen. is there a way of altering the size of firefox when it opens?
browser = webdriver.Firefox()
#enter website into searchbox
browser.get('https://website')
#find the password element in page source
inputElement = browser.find_element_by_id("password")
#input users password into website
inputElement.send_keys(password)
inputElement.send_keys(Keys.RETURN)
Maximize (Full screen) the Firefox window as below:
browser.maximize_window()