I'm trying to login to this website using Selenium:
https://www.gamingintelligence.com/my-account
I've done this so far:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
username = 'my_user'
password = 'my_pass'
chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='./chromedriver')
url = "https://www.gamingintelligence.com/my-account"
driver.get(url)
driver.find_element_by_xpath('//*[#id="nav-login-tab"]').click()
driver.find_element_by_xpath('//*[#id="username"]').send_keys(username)
But I get this error:
ElementNotInteractableException: Message: element not interactable
The HTML is:
<input type="text" class="form-control xh-highlight" name="username"
id="username" autocomplete="username" value="" required="" style="background-
image: ...">
To solve exactly this issue you'll need to add explicit wait:
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
username = 'my_user'
password = 'my_pass'
chrome_options = Options()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(chrome_options=chrome_options, executable_path='/snap/bin/chromium.chromedriver')
url = "https://www.gamingintelligence.com/my-account"
driver.get(url)
wait = WebDriverWait(driver, 20)
wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="nav-login-tab"]')))
driver.find_element_by_xpath('//*[#id="nav-login-tab"]').click()
field = wait.until(EC.element_to_be_clickable((By.XPATH, '//*[#id="username"]')))
driver.find_element_by_xpath('//*[#id="username"]').send_keys(username)
Or, you can set implicit wait, just after driver.get():
driver.implicitly_wait(15)
First way is more reliable.
Related
What I want to do:
Build a tweet bot for twitter.
My problem:
My program is unable to find the text box to enter my username, even when I use explicit wait.
What I've tried:
Locating the element by class
Locating the element by name
Locating the element by xpath
Using explicit wait to make sure the element is on the screen before program continues.
My code:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# getting webdriver path
s = Service('/Users/shannonslater/Desktop/Everything/Dev/WebDrivers/chromedriver101')
driver = webdriver.Chrome(service=s)
# navigating to twitter login page
driver.get('https://twitter.com/login')
# trying to click on the username text box
try:
username_text_box = WebDriverWait(driver, 20).until(
EC.presence_of_element_located(By.XPATH, '//*[#id="layers"]/div/div/div/div/div/div/div[2]/div[2]/div/div/div[2]/div[2]/div[1]/div/div/div[5]/label/div/div[2]/div/input')
)
username_text_box.click()
except:
print("could not find username_text_box element")
# username text box is never clicked and "could not find username" is always printed
I am copying the xpath directly from the inspected html element:
The username field on Twitter Login Page is a dynamic element.
<input autocapitalize="sentences" autocomplete="username" autocorrect="on" name="text" spellcheck="true" type="text" dir="auto" class="r-30o5oe r-1niwhzg r-17gur6a r-1yadl64 r-deolkf r-homxoj r-poiln3 r-7cikom r-1ny4l3l r-t60dpp r-1dz5y72 r-fdjqy7 r-13qz1uu" value="">
Solution
To send a character sequence to the element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following locator strategies:
Using CSS_SELECTOR:
options = Options()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://twitter.com/i/flow/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "input[autocomplete='username']"))).send_keys("ShannonSlater")
Using XPATH:
options = Options()
options.add_argument("start-maximized")
s = Service('C:\\BrowserDrivers\\chromedriver.exe')
driver = webdriver.Chrome(service=s, options=options)
driver.get("https://twitter.com/i/flow/login")
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[#autocomplete='username']"))).send_keys("ShannonSlater")
Note: You have to add the following imports :
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
Browser Snapshot:
In this code I am trying to scrape a Linkedin profile using Selenium
but the driver is not able to load the page I guess IP has been
blocked and I am new to the concept of proxy rotating or any concept
that is used in such cases. It would be a great help if you could help
me understand how this is done.
from selenium import webdriver
import time
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.Chrome(executable_path=r'C:\Users\chromedriver.exe')
def linkedin_login():
global driver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option('excludeswitches', ['enable-automation'])
options.add_experimental_option("detach", True)
try:
driver.get('https://www.linkedin.com/login')
username = 'username'
password = 'password'
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys(username)
driver.find_element_by_id('password').send_keys(password)
driver.find_element_by_class_name('btn__primary--large from__button--floating').click()
time.sleep(8)
except ImportError:
print('Closing')
def search_profiles():
search_profile = input('What profile do you want to search?')
search_profile = search_profile.split()
search = search_profile[0] + "%20" + search_profile[1]
The "Sign In" button's class name is incorrect, specifically is missing a dot(.)
Instead of:
driver.find_element_by_class_name('btn__primary--large from__button--floating').click()
Use:
driver.find_element_by_class_name('btn__primary--large.from__button--floating').click()
That will click the button.
Also, if you run the code you shared, you are calling the webdriver but not calling your function.
I tested the below code and worked fine (remember to update your path and LinkedIn credentials):
from selenium import webdriver
import time
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.Chrome(executable_path='/home/armentaahumada/Downloads/chromedriver')
def linkedin_login():
global driver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
options.add_experimental_option('excludeswitches', ['enable-automation'])
options.add_experimental_option("detach", True)
try:
driver.get('https://www.linkedin.com/login')
username = 'username'
password = 'password'
WebDriverWait(driver, 30).until(EC.element_to_be_clickable((By.ID, 'username'))).send_keys(username)
driver.find_element_by_id('password').send_keys(password)
driver.find_element_by_class_name('btn__primary--large.from__button--floating').click()
time.sleep(8)
except ImportError:
print('Closing')
def search_profiles():
search_profile = input('What profile do you want to search?')
search_profile = search_profile.split()
search = search_profile[0] + "%20" + search_profile[1]
linkedin_login()
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "/Users/khizarm/Downloads/chromedriver"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(PATH, chrome_options=chrome_options)
driver.get("facebook.com/login/")
email = driver.find_element_by_id('email')
email.send_keys('MY EMAIL')
driver.find_element_by_id('pass').send_keys('MY PASSWORD')
driver.find_element_by_name('login').click()
Exception:
selenium.common.exceptions.ElementClickInterceptedException: Message: element click intercepted: Element <button value="1" class="_42ft _4jy0 _52e0 _4jy6 _4jy1 selected _51sy" id="loginbutton" name="login" tabindex="0" type="submit">...</button> is not clickable at point (600, 341). Other element would receive the click: <div>...</div>
(Session info: chrome=91.0.4472.77)
You need to wait until fields where you input data are clickable.
Also, you need to input an email in a correct format.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
PATH = "/snap/bin/chromium.chromedriver"
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--incognito")
driver = webdriver.Chrome(PATH, chrome_options=chrome_options)
driver.get("https://www.facebook.com/login")
wait = WebDriverWait(driver, 30)
wait.until(EC.element_to_be_clickable((By.ID, "email")))
email = driver.find_element_by_id('email')
email.send_keys('myemail#sometest.com')
driver.find_element_by_id('pass').send_keys('MY PASSWORD')
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "#loginbutton")))
driver.find_element_by_css_selector('#loginbutton').click()
You can also use driver.find_element_by_id with loginbutton locator.
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 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">