Python User/Pass autofill selenium - python

I was following along with a tutorial to create the below program to
auto log into a github account. When I run the program it takes me to the sign in
page but does not not autofill the fields and submit. The tutorial was using "find_element_by_id" method. However, it was not recognized. After some googling I came up with the "By" class and it seems to work well earlier in the program when I used "By.LINK_TEXT" but fails when using it for .ID?
or maybe it is an issue with the "send_keys" method?
Any assistance would be greatly appreciated.
from selenium.webdriver.common.keys import Keys
import re
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_experimental_option("detach", True)
browser = webdriver.Chrome(
options=options, executable_path="C:\\Windows\\chromedriver.exe")
browser.get("https://github.com")
signin_link = browser.find_element(By.LINK_TEXT, "Sign in")
signin_link.click()
username_box = browser.find_element(By.ID, "login_field")
username_box.send_keys("username")
password_box = browser.find_element(By.ID, "password")
password_box.send_keys("password")
password_box.submit()
`
I tried the various constants for the By class (name, id, link_text, etc.)
None of them seems to make any difference. I imagine the problem is somewhere else in the program.

Add WebDriveWait so the page can load before trying to pass the username and password
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import re
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_experimental_option("detach", True)
browser = webdriver.Chrome(
options=options, executable_path="C:\\Windows\\chromedriver.exe")
browser.get("https://github.com")
signin_link = browser.find_element(By.LINK_TEXT, "Sign in")
signin_link.click()
# wait until the page loads
WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'login_field')))
username_box = browser.find_element(By.ID, "login_field")
username_box.send_keys("username")
password_box = browser.find_element(By.ID, "password")
password_box.send_keys("password")
password_box.submit()

Related

How to separately send email to the form

I made web contact form, my email sending to subscribe email box , I want to send email to the form only. Please help me
driver.get('https://shop.rtrpilates.com/')
driver.find_element_by_partial_link_text('Contact'),click
try:
username_box = driver.find_element_by_xpath('//input[#type="email"]')
username_box.send_keys("n#gmail.com")
I don't understand how can I create a block between this, Help please
Advance thanks
Seems you main problem here is that you trying to use deprecated methods find_element_by_*. None of these is supported by Selenium 4.
Also code you shared is missing delays to wait for elements to become clickable etc.
The following short code works:
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
options = Options()
options.add_argument("start-maximized")
webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 10)
url = "https://shop.rtrpilates.com/"
driver.get(url)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".header__inline-menu a[href*='contact']"))).click()
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".contact__fields input[type='email']"))).send_keys("n#gmail.com")
The result is

Why isn't the webpage loding while scraping linkedin?

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

Auto Login Google Account Python Selenium

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

Why does my Selenium URL_to_be statement not work?

Selenium does not seem to register that I manually go to the publish0x.com page.
Does anyone know a solution?
My goal is to manually do the captcha at the login page and afterwards, when I log in and land on the main page I want the script to resume.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
import datetime
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.common.exceptions import TimeoutException
import sys
from selenium.webdriver.support.ui import WebDriverWait
def waitForLoad(inputXPath):
Wait = WebDriverWait(driver, 10)
Wait.until(EC.presence_of_element_located((By.XPATH, inputXPath)))
email = '
password = '
options = Options()
options.add_experimental_option("detach", True)
options.add_argument("--window-size=1920,1080")
## options.add_argument("user-data-dir=/Users/vadim/Library/Application Support/BraveSoftware/Brave-Browser")
options.binary_location = '/Applications/Brave Browser.app/Contents/MacOS/Brave Browser'
driver_path = '/usr/local/bin/chromedriver'
driver = webdriver.Chrome(options=options, executable_path=driver_path)
driver.get('https://www.publish0x.com/login')
waitForLoad('//*[#id="email"]')
E_Mail_vak = driver.find_element_by_xpath('//*[#id="email"]')
E_Mail_vak.send_keys(email)
Pass_vak = driver.find_element_by_xpath('//*[#id="password"]')
Pass_vak.send_keys(password)
frame = driver.find_element_by_xpath('//iframe[contains(#src, "recaptcha")]')
driver.switch_to.frame(frame)
Captcha = driver.find_element_by_xpath("//*[#id='recaptcha-anchor']")
Captcha.click()
wait = WebDriverWait(driver, 500)
wait.until(EC.url_to_be("publish0x.com"))
driver.get('https://www.publish0x.com/newposts')
post = driver.find_element_by_css_selector('#main > div.infinite-scroll > div:nth-child(1) > div.content')
title = post.find_element_by_css_selector('h2 > a').text
author = post.find_element_by_css_selector('p.text-secondary > small:nth-child(4) > a').text
title.click()
slider = driver.find_element_by_xpath('//*[#id="tipslider"]')
There are two ways I can think of, one by adding an Input statement like this:
options.add_argument('--disable-gpu')#For properly seeing the outputs
input("Please do the captcha and press any key...)
In this way, the user would complete the data and then press any key for the script to continue.
The other way is by adding a try and except statement.
try:
driver.find_element_by_id("some-element")
except NoSuchElementException:
#do something like
print("Captcha Failed or Incomplete...")
In this, replace the element id "some-element" with any element that is present and only present after the user logs in, for e.g elements like Profile_nav or Settings are only present when someone logs in. So if the element doesn't exist then it would mean that the user didn't complete the captcha.

How to send character sequence to the username and password field within the url through Selenium?

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:

Categories