I am trying to log into gmail using Selenium. In the new gmail log in, first you type your email id and then a next page comes where you type your password. URL of email page and password page, both are different. So, when I am passing the password URL in driver.get it is reloading the page and it redirects to email page if you refresh the URL without entering password. Because of this, it is missing the password field selector. current_url is still the previous url, i.e, url of email page. This is my code. I am using chrome driver and python 2.X
import os
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
chromedriver = "/Documents/chromedriver" # Path to chrome-driver
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
# Email insert
driver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin") #URL of email page
username = driver.find_element_by_id("identifierId")
username.send_keys("myemail")
driver.find_element_by_id("identifierNext").click()
# Password Insert
driver.get("https://accounts.google.com/signin/v2/identifier?service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F&ss=1&scc=1<mpl=default<mplcache=2&emr=1&osid=1&flowName=GlifWebSignIn&flowEntry=ServiceLogin") # URL of password page
password = driver.find_element_by_id("password")
password.send_keys("mypassword")
driver.find_element_by_id("passwordNext").click()
#driver.quit()
Here is the Answer to your Question:
When we work with Selenium 3.4.3, geckodriver v0.17.0 and Mozilla Firefox 53.0 using Python 3.6.1, we can use either of the locators xpath or css_selector to log into our respective Gmail accounts through Gmail's signin module v2.
Using XPATH :
Here is the sample code to log into Gmail using xpath:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_xpath("//input[#id='identifierId']")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_id("identifierNext").click()
password = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.XPATH, "//input[#name='password']"))
)
password.send_keys("your_password")
driver.find_element_by_id("passwordNext").click()
Using CSS_SELECTOR:
Here is the sample code to log into Gmail using css_selector:
from selenium import webdriver
from selenium.webdriver.firefox.firefox_binary import FirefoxBinary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
binary = FirefoxBinary('C:\\Program Files\\Mozilla Firefox\\firefox.exe')
caps = DesiredCapabilities().FIREFOX
caps["marionette"] = True
driver = webdriver.Firefox(capabilities=caps, firefox_binary=binary, executable_path="C:\\Utility\\BrowserDrivers\\geckodriver.exe")
driver.get("https://accounts.google.com/signin")
email_phone = driver.find_element_by_css_selector("#identifierId")
email_phone.send_keys("your_emailid_phone")
driver.find_element_by_css_selector(".ZFr60d.CeoRYc").click()
password = WebDriverWait(driver, 5).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "input[class='whsOnd zHQkBf'][type='password']"))
)
password.send_keys("your_password")
driver.find_element_by_css_selector(".ZFr60d.CeoRYc").click()
<input class="whsOnd zHQkBf"
jsname="YPqjbf"
autocomplete="current-password"
spellcheck="false"
tabindex="0"
aria-label="Enter your password"
name="password"
autocapitalize="off"
autocorrect="off"
dir="ltr"
data-initial-dir="ltr"
data-initial-value=""
badinput="false"
type="password">
Related
I was wondering if there is anyway to run selenium with a current session of a browser? Currently, I have a script that uses selenium but once it logs in, the site sends a two-factor authentication since the browser appears to be a new login point. I want to have the script that just updates the fields without having to log in, and just open a new tab of a current browser that is already logged in chrome or firefox:
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
# driver = webdriver.Firefox()
# driver = webdriver.Firefox(executable_path=executable_path, firefox_options=options, log_path=os.devnull)
url="https://robinhood.com/"
driver = webdriver.Chrome("driver location")
driver.get(url)
loginBtn = driver.find_element_by_class_name('css-1tfdro-UnstyledAnchor-Button')
loginBtn.click()
# username = driver.find_element_by_name('username')
def login_rh(usernamef, username, passwordf, password):
username_field = WebDriverWait(driver, 10). until(EC.presence_of_element_located((By.NAME, usernamef)))
username_field.send_keys(username)
password_field = WebDriverWait(driver, 10). until(EC.presence_of_element_located((By.CLASS_NAME, passwordf)))
password_field.send_keys(password)
submit_button= WebDriverWait(driver, 10). until(EC.presence_of_element_located((By.CLASS_NAME, "class")))
submit_button.click()
login_rh("username", "test#gmail.com", "css-a4852m", "passtest")
Type :
**chrome://version/** in browser you will get executable path of chrome.exe
copy this and close all chrome instance.
now start edge as:
"C:\Program Files (x86)\Microsoft\chrome\Application\chrome.exe" --remote-debugging-port=5555
Now add remote debugging port as:
options = webdriver.ChromeOptions()
options.add_experimental_option("debuggerAddress", "127.0.0.1:5555");
webdriver.Chrome(options=options)
Can't find any solution for auto login google (gmail) account.
Here is my code :
import os
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
import time
opt = Options()
executable_path = r'chromedriver'
os.environ["webdriver.chrome.driver"] = executable_path
opt.add_extension(r'C:\Users\SAMSUNG\Desktop\SB.crx')
opt.add_extension(r'C:\Users\SAMSUNG\Desktop\proxy.crx')
opt.add_argument('--disable-blink-features=AutomationControlled')
opt.add_experimental_option("excludeSwitches", ["enable-automation"])
opt.add_experimental_option('useAutomationExtension', False)
opt.add_argument("window-size=1280,800")
usernameStr = 'x'
passwordStr = 'y'
driver = webdriver.Chrome(r'C:\Users\SAMSUNG\Desktop\chromedriver', options=opt)
time.sleep(5)
driver.get(('https://accounts.google.com/ServiceLogin?'
'service=mail&continue=https://mail.google'
'.com/mail/#identifier'))
# fill in username and hit the next button
username = driver.find_element_by_id('identifierId')
username.send_keys(usernameStr)
time.sleep(2)
nextButton = driver.find_element_by_id('identifierNext')
nextButton.click()
# wait for transition then continue to fill items
password = WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.NAME, "password")))
password.send_keys(passwordStr)
signInButton = driver.find_element_by_id('passwordNext')
signInButton.click()
But i keep getting this error : 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.
ERROR
I am trying to post a tweet in Twitter using python and selenium
But at the time of posting it is giving me timeout error.
I have the code ready....
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
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 import webdriver
import time
import os
# create a new Chrome session
chromedriver = "C:/Users/LENOVO/Desktop/chromedriver/chromedriver.exe"
chromedriver = "C:/Users/LENOVO/Desktop/chromedriver/chromedriver.exe"
os.environ["webdriver.chrome.driver"] = chromedriver
driver = webdriver.Chrome(chromedriver)
#driver = webdriver.Chrome()
driver.implicitly_wait(30)
driver.maximize_window()
# navigate to the application home page
driver.get("https://twitter.com/login")
# get the username textbox
login_field = driver.find_element_by_class_name("js-username-field")
login_field.clear()
# enter username
login_field.send_keys("")
time.sleep(1)
#get the password textbox
password_field = driver.find_element_by_class_name("js-password-
field")
password_field.clear()
#enter password
time.sleep(10)
password_field.send_keys("")
time.sleep(10)
password_field.submit()
autotw1 = WebDriverWait(driver, 140).until(
EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id='tweet-box-home-
timeline']")))
autotw1.send_keys("""Just a testing """)
tweet = driver.find_element_by_xpath("//span[#class='add-tweet-button
']//following-sibling::button[contains(#class,'tweet-action')]")
tweet.click()
I am getting the below error.
TimeoutException Traceback (most recent call last)
in
39
40 autotw1 = WebDriverWait(driver, 140).until(
---> 41 EC.element_to_be_clickable((By.CSS_SELECTOR, "div[id='tweet-box-home-timeline']")))
You can use this locator:By.CLASS_NAME, 'DraftEditor-root'.
You must click on the element to bring up other elements to write the tweet, which is:By.CLASS_NAME, 'public-DraftEditorPlaceholder-root'), and use ActionChains to send text.
First, following import:
from selenium.webdriver import ActionChains
After submit login please try below:
password_field.submit()
autotw1 = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CLASS_NAME, 'DraftEditor-root')))
autotw1.click()
element = WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.CLASS_NAME, 'public-DraftEditorPlaceholder-root')))
ActionChains(driver).move_to_element(element).send_keys("""Just a testing """).perform()
sendTw = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, '//div[#role="button"]/div/span/span')))
sendTw.click()
You have provided incorrect xpath and css value in your script. Kindly run below scrip with your own credentials and don't forget to update chrome driver path.
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
import time
from selenium.webdriver.support.ui import WebDriverWait as Wait
from selenium.webdriver.common.action_chains import ActionChains
# Open Chrome
driver = webdriver.Chrome('Path to chrome driver')
driver.get("https://twitter.com/login")
# get the username textbox
login_field = driver.find_element_by_class_name("js-username-field")
login_field.clear()
# enter username
login_field.send_keys("")
time.sleep(1)
# get the password textbox
password_field = driver.find_element_by_class_name("js-password-field")
password_field.clear()
# enter password
password_field.send_keys("")
password_field.submit()
autotw1 = WebDriverWait(driver, 15).until(EC.visibility_of_element_located((By.XPATH, "//div[#class='css-1dbjc4n r-xoduu5 r-1sp51qo r-mk0yit r-13qz1uu']")))
ActionChains(driver).move_to_element(autotw1).click(autotw1).send_keys("Just a testing").perform()
tweet = WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//span[#class='css-901oao css-16my406 css-bfa6kz r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0']//span[#class='css-901oao css-16my406 r-1qd0xha r-ad9z0x r-bcqeeo r-qvutc0'][contains(text(),'Tweet')]")))
tweet.click()
I am trying to send keys via selenium it is taking for username but not for password.
I have tried clicking and sending keys then.
HTML of password field:
<div>
<input name="txtPassword" type="password" id="txtPassword" required="" style="margin-bottom:0px;" class="blur">
<a id="btnSmallForgotPassword" class="smallForgotPassword visible-sm-block" href="javascript:__doPostBack('btnSmallForgotPassword','')">forgot password</a>
</div>
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID,"txtPassword"))).click()
WebDriverWait(driver, 3).until(EC.visibility_of_element_located((By.ID,"txtPassword"))).send_keys("san")
I am getting no error message but it is not sending keys for password
try with this code :
WebDriverWait(driver, 3).until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
You should not create object of WebDriverWait too many times, use it like this :
wait = WebDriverWait(driver,10)
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
and wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))) returns a web element, where you can use methods like click(), clear(), send_keys() etc.
You can write your code like this also :
password_field = wait.until(EC.element_to_be_clickable((By.ID,"txtPassword")))
password_field.click()
password_field.send_keys('your_password')
EDIT1 :
You can use this css selector :
input[id='txtPassword']
EDIT 2 :
You can use this full method :
wait = WebDriverWait(driver,10)
driver.maximize_window()
driver.get("http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7")
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input[id='txtUser']"))).send_keys("abhishek")
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).click()
wait.until(EC.element_to_be_clickable((By.ID,"txtPassword"))).send_keys('your_password')
Try this:
from selenium import webdriver
import time
driver = webdriver.Chrome('/usr/bin/chromedriver')
driver.get('http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7')
time.sleep(3)
username = driver.find_element_by_id("txtUser")
username.clear()
# insert username
username.send_keys("mrcats")
password = driver.find_element_by_name("txtPassword")
password.clear()
# insert password
password.send_keys("catskillz")
login = driver.find_element_by_name("btnLogin")
#click on login button
login.click()
The above code should work.However I have tested following code on chrome browser and working fine.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium import webdriver
driver = webdriver.Chrome()
driver.get('http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7')
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.ID,'txtUser'))).send_keys("Abhishek")
WebDriverWait(driver,20).until(EC.element_to_be_clickable((By.CSS_SELECTOR,'div.divInnerRightControls #txtPassword'))).send_keys("Abhishek")
Output:
To send character sequence to the USER ID and PASSWORD field you need to to induce WebDriverWait for the element to be clickable and you can use the following Locator Strategy:
Code Block:
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
driver = webdriver.Firefox(executable_path=r'C:\Utility\BrowserDrivers\geckodriver.exe')
driver.get("http://backgriduk.medialava.com/pages/Staff/Login.aspx?LANGUAGE_ID=3&O=7")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#txtUser"))).send_keys("abhishek_gupta")
driver.find_element_by_css_selector("input#txtPassword").send_keys("abhishek_gupta")
Browser Snapshot:
I tried to log in in https://login.economicmodeling.com/login/login.php, but the username I put in doesn't show up, and after I run the command to fill in password, it automatically opened a new tab without actually filling in password. Anyone can help? Thanks!
from selenium import webdriver
driver = webdriver.Safari()
driver.get("https://login.economicmodeling.com/login/login.php")
driver.find_element_by_class_name("cc-btn cc-dismiss").click()
user = driver.find_element_by_css_selector('input[name = user]')
password = driver.find_element_by_css_selector('input[name = password]')
user.clear()
user.send_keys('xxx')
password.clear()
password.send_keys('xxx')
driver.find_element_by_id("submitbutton").click()
Missing single quotes around 'user' and 'password' attributes.
user = driver.find_element_by_css_selector("input[name='user']")
password = driver.find_element_by_css_selector("input[name='password']")
Sending the form should be done with submit, not click.
driver.find_element_by_id("submitbutton").submit()
As per your question to login in https://login.economicmodeling.com/login/login.php you need to induce WebDriverWait for the desired elements to be clickable and you can use the following solution:
Code Block:
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
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_argument('disable-infobars')
driver=webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
driver.get("https://login.economicmodeling.com/login/login.php")
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input.fancy.form#userinput"))).send_keys('xxx')
driver.find_element_by_css_selector('input.fancy.form#passwordinput').send_keys('xxx')
driver.find_element_by_css_selector("input.submit.button.success#submitbutton").click()
Browser Snapshot: