In this super basic python script using Selenium, I am just trying to automate my twitter login so I can begin scraping. When the chrome session opens, the username is filled out, but the password field is left blank.
import bs4
from selenium import webdriver
driver = webdriver.Chrome();
url = "https://twitter.com/login"
driver.get(url)
assert "Twitter" in driver.title
username = driver.find_element_by_class_name('js-username-field')
username.send_keys('example_username')
password = driver.find_element_by_class_name('js-password-field')
password.clear()
password.send_keys('exmaple_password')
login_button = driver.find_element_by_css_selector("button.submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium")
login_button.submit()
It's possible you need to add a wait on the password field. This can help if you are seeing intermittent issues with your script. I prefer to wait on an element rather than sleep.
import bs4
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Chrome();
url = "https://twitter.com/login"
driver.get(url)
assert "Twitter" in driver.title
username = driver.find_element_by_class_name('js-username-field')
username.send_keys('example_username')
# wait on password field
WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, 'js-password-field')))
password = driver.find_element_by_class_name('js-password-field')
password.clear()
password.send_keys('exmaple_password')
login_button = driver.find_element_by_css_selector("button.submit.EdgeButton.EdgeButton--primary.EdgeButtom--medium")
login_button.submit()
Python moves through steps pretty quickly, and it takes a while for web pages to catch up sometimes. Adding a wait helps slow things down a bit, and can fix some intermittent errors you might be seeing.
Related
0
I am trying to do a tutorial and learn Selenium in python however i cant seem to get Selenium to enter the password into the password field/form box. It enters the email addres perfectly fine, and inside the code the password is typed correctly however the website returns it as "Incorrect password entered". However when i type the password in manually it logs in as it is correct. I have removed the email and password for security
I am using:
Python v3.9
Chrome v87
This is the URL i am practicing on:
https://www.aria.co.uk/myAria/ShoppingBasket?action=checkout
And this is my current code:
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
from selenium.webdriver.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\Ste1337\Desktop\chromedriver\chromedriver.exe")
# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[email]")))
login.send_keys("testuser#hotmail.co.uk")
login.send_keys(Keys.TAB)
driver.implicitly_wait(2)
login.send_keys("password")
driver.implicitly_wait(2)
login.send_keys(Keys.ENTER)
You haven't defined somewhere to put the password. Currently, the password in MEANT to be entered in the same place as the email. To change that, define where the password is meant to go. This is how I did it:
passwrd = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/div/h1/div[3]/div/div/input[2]")
passwrd.send_keys("password")
So in total, the code should look like this:
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
from selenium.webdriver.common.action_chains import ActionChains
import time
# Open Chromedriver
driver = webdriver.Chrome(r"C:\Users\eesam\AppData\Local\Programs\Python\Python39\Scripts\chromedriver.exe")
# Open webpage
driver.get("https://www.aria.co.uk/SuperSpecials/Other+products/ASUS+ROG+Pugio+2+Wireless+Optical+RGB+Gaming+Mouse?productId=72427")
#https://www.aria.co.uk/Products/Components/Graphics+Cards/NVIDIA+GeForce/GeForce+RTX+3060+Ti/Palit+GeForce+RTX+3060+Ti+Dual+8GB+GPU?productId=73054
# Click "Add to Basket" or refresh page if out of stock
try:
element = WebDriverWait(driver, 1).until(EC.presence_of_element_located((By.XPATH, "Out of Stock!")))
time.sleep(5)
browser.refresh()
except:
button = driver.find_element_by_id("addQuantityButton")
button.click()
basket = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.ID, "basketContent")))
basket.click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.XPATH, "//img[contains(#src,'/static/images/checkoutv2.png')]"))).click()
login = WebDriverWait(driver,10).until(EC.element_to_be_clickable((By.NAME,"login[email]")))
login.send_keys("testuser#hotmail.co.uk")
login.send_keys(Keys.TAB)
driver.implicitly_wait(2)
passwrd = driver.find_element_by_xpath("/html/body/div[4]/div[1]/div[2]/form/div/h1/div[3]/div/div/input[2]")
passwrd.send_keys("password")
driver.implicitly_wait(2)
login.send_keys(Keys.ENTER)
I'm working on a online purchase bot for target.com and I've run into a blocker. Upon providing correct username and password combination on the login page and clicking "login", I get the following error message on the target login page: "Sorry, something went wrong. Please try again.". This only occurs when running through browser automation. Just wondering if there's a workaround for this issue. Here is my code thus far:
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
import time
PATH="C:\Program Files (x86)\chromedriver.exe"
driver = webdriver.Chrome(PATH)
btn_sign_in_nav = "//span[text()='Sign in']"
btn_sign_in_drop_down = "//div[#id='accountMenu']//div[text()='Sign in']"
input_username_login = "//input[#id='username']"
input_password_login = "//input[#id='password']"
btn_submit_login = "//button[#id='login']"
def xpath_explicit_wait(xpath_val, time_sec):
if type(time_sec) != int or type(xpath_val) != str:
print("NUMERICAL VALUES ONLY!")
driver.quit()
try:
element = WebDriverWait(driver, time_sec).until(
EC.presence_of_element_located((By.XPATH, xpath_val))
)
except NoSuchElementException:
driver.quit()
def login_user(url, username, password):
driver.get(url)
driver.implicitly_wait(3)
driver.find_element_by_xpath(btn_sign_in_nav).click()
xpath_explicit_wait(btn_sign_in_drop_down, 5)
driver.find_element_by_xpath(btn_sign_in_drop_down).click()
driver.implicitly_wait(3)
driver.find_element_by_xpath(input_username_login).send_keys(username)
driver.find_element_by_xpath(input_password_login).send_keys(password)
xpath_explicit_wait(btn_submit_login, 5)
driver.find_element_by_xpath(btn_submit_login).click()
There are detection mechanisms on sites like target that detect when you're using selenium and prevent the site from working.
More details can be found in the answer here: Can a website detect when you are using Selenium with chromedriver?
PhoenixBot implements a mechanism that changes the contents of the driver so that it's undetectable. Use this same mechanism and your problems will vanish, as mine did! :-D
https://github.com/Strip3s/PhoenixBot/blob/554441b3b6888a9be46b8aed6d364dc33da92e87/utils/selenium_utils.py#L150-L173
There is a bot Phoenixbot, I can't speak for the functionality 100% but the auto login portion definitely works and is python. https://github.com/Strip3s/PhoenixBot/blob/master/sites/target.py
I've been attempting to figure out how exactly it's accomplishing that but no success replicating successfully in my own code. Maybe take a look.... If you figure it out I'd love to know.
IF you have the ability to use Safari webdriver, then see the below code.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as ec
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Safari(executable_path='/usr/bin/safaridriver')
driver.get('https://www.target.com/')
action = ActionChains(driver)
driver.find_element(By.XPATH, '//*[#id="account"]').click()
WebDriverWait(driver, 30).until(ec.presence_of_element_located((By.XPATH, '//*[#id="accountNav-signIn"]')))
action.send_keys(Keys.ENTER)
action.perform()
WebDriverWait(driver, 10).until(ec.presence_of_element_located((By.XPATH, '//h2[#class="sc-hMqMXs sc-esjQYD eXTUDl"]')))
driver.find_element(By.ID, 'username').click()
driver.find_element(By.ID, 'username').send_keys('foo')
time.sleep(5)
driver.find_element(By.ID, 'password').click()
driver.find_element(By.ID, 'password').send_keys('bar')
time.sleep(5)
driver.find_element(By.XPATH, "//button[#id=\'login\']").send_keys(Keys.ENTER)
time.sleep(10)
driver.quit()
As #Decian Shanaghy mentioned, Target seems to be bot protected, but Safari webdriver still works.
The time.waits are not needed, you can remove them if you would like.
For anyone still seeking a solution to this, see this answer at Can a website detect when you are using Selenium with chromedriver?. I did a search through the chromedriver binary executable (MacOS in my case) and changed instances of $cdc_ to $abc_.
I originally suspected Target employs a JavaScript solution to hunting selenium users. I confirmed this suspicion by using selenium to open a browser at https://www.target.com without any automation. I attempted to manually login and obtained a 401 response with the well known "Sorry, something went wrong. Please try again." That didn't rule out a UI-based bot hunting solution but it pointed to a JavaScript hunter.
I'm trying to log into my twitter account by using selenium. The filling of username and password is working perfectly, but when pressing the login-button nothing happens.
self.driver.find_element_by_xpath('//*[#id="react-root"]/div/div/div[2]/main/div/div/form/div/div[3]/div').click()
I looked into the html-code and the aria-haspopup is on false. Is there any way I can set it on true so I can click the button?
Greets
Your xpath is working well on twitter's login page (https://twitter.com/login). Maybe you're trying to login on other page.
I'm providing small piece of code for logging into twitter. I used another xpaths and WebDriverWait for waiting until login form would be displayed.
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
driver = webdriver.Chrome()
driver.get('https://twitter.com/login')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.XPATH, "//input[#name='session[username_or_email]' and #data-focusable='true']")))
driver.find_element_by_xpath("//input[#name='session[username_or_email]' and #data-focusable='true']").send_keys('login')
driver.find_element_by_xpath("//input[#name='session[password]' and #data-focusable='true']").send_keys('password')
driver.find_element_by_xpath("//div[#data-testid='LoginForm_Login_Button']").click()
Try to do it like this:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('https://twitter.com/login')
email = driver.find_element(By.NAME, 'email')
email.clear()
email.send_keys(EMAIL)
password = driver.find_element(By.NAME, 'pass')
password.clear()
password.send_keys(PASSWORD)
password.send_keys(Keys.RETURN) # Login with ENTER button
So i automated the task of login into a discordserver and posting a text using Selenium. All works fine but my goal is to make an inputbox for the email and password. The user inputs the information and presses save and the password and mail is supposed to write itsself into the code and save.
What are the best approaches for this? I'm still pretty new to python so go easy on me.
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from time import sleep
driver = webdriver.Firefox()
url = "https://discordapp.com/channels/530588470905929729/538868623981412362"
driver.get(url)
email = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='email']")))
email.send_keys("Mail#mail.com")
password = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//input[#type='password']")))
password.send_keys("Password" + Keys.ENTER)
sleep(5)
textbox = WebDriverWait(driver,10).until(EC.presence_of_element_located((By.XPATH,"//textarea[#placeholder='Message #bot-commands']")))
textbox.send_keys("!work" + Keys.ENTER)
sleep(30)
driver.quit()
I'm trying to use auto login in to a site using selenium through pyhton but it throws
http: 405 error "Pardon our Interruption. something about your browser made us think you were a bot"
What can I do to avoid it? I would like to see the execution live to check if the code is working correctly which I can't do if I use it in headless mode. Am I wrong?
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
usernameStr = 'bucmi1#yandex.com'
passwordStr = 'pmz4'
browser = webdriver.Chrome()
browser.get(('https://www.milanuncios.com/mis-anuncios/'))
# fill in username and strike a subsequent button
username = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'email')))
username.send_keys(usernameStr)
# wait for transition then continue to fill items
password = WebDriverWait(browser, 10).until(
EC.presence_of_element_located((By.ID, 'contra')))
password.send_keys(passwordStr)
signInButton = browser.find_element_by_class_name('submit btnSend')
signInButton.click()
Thanks in advance.
It is not clear why you got an error message as :
http: 405 error "Pardon our Interruption. something about your browser made us think you were a bot"
But I was able to successfully able to login through the credentials you provided with your own code adding some minor tweaks through chrome.options in incognito mode as follows :
Code Block :
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
usernameStr = 'bucmi1#yandex.com'
passwordStr = 'pmz4'
options = Options()
options.add_argument("start-maximized")
options.add_argument("disable-infobars")
options.add_argument("--disable-extensions")
options.add_argument('--incognito')
browser = webdriver.Chrome(chrome_options=options, executable_path=r'C:\Utility\BrowserDrivers\chromedriver.exe')
browser.get(('https://www.milanuncios.com/mis-anuncios/'))
# fill in username and strike a subsequent button
username = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'email')))
username.send_keys(usernameStr)
# wait for transition then continue to fill items
password = WebDriverWait(browser, 10).until(EC.presence_of_element_located((By.ID, 'contra')))
password.send_keys(passwordStr)
signInButton = browser.find_element_by_css_selector("div.btnEnviarFrm>input.submit.btnSend[value^='INICIAR']")
signInButton.click()
Browser Snapshot :
Thanks for the answers! The problem had something to do with my default profile folder. Don't know exactly what since there are many files I don't understand. But once I created a new one using options.add_argument("user-data-dir=C:\\dir\\of\\example\\profile")
could access without problems.