AttributeError: 'dict' object has no attribute 'send_keys' Selenium Webdriver - python

I'm relatively new to coding and python. I'm trying to automate logging into linkedin to send messages to my connections. I'm using selenium webdriver for this process. I haven't been able to log in yet with the automated process because I'm getting
the error: dict object has no attribute send_keys.
I know in this code 'username' is a dictionary type because I checked and the error is telling me it has no attribute 'send_keys', I get what the error message is saying, that the attribute does not exist, but I don't know how to fix it. I'd also like to ask the variable I've created called 'username' can I call that anything? I know calling it username is probably the best, but I'm asking this for my understanding.
The following code is what I have done so far, I know it's not complete but I like to work and fix issues one line at a time.
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
import time
s = Service("/usr/local/bin/chromedriver")
driver = webdriver.Chrome(service=s)
driver.get("https://linkedin.com/login")
time.sleep(2)
username = driver.find_element(By.ID,"username")
username.send_keys("my email address goes here")
I'm also attaching an image so it can be seen what part of the LinkedIn page and tags I'm using to try to log in.
Linkedin inspect element code on signing page
I hope I haven't left anything out, I tried to be as descriptive as possible.
Thanks in advance!

This is a bug!
The method webdriver.find_element() is supposed to return an object webdriver.remote.webelement.WebElement and not a dictionary.
Hence, this behaviour is most likely a bug as documented here and not a coding error of yours.
You might be using an old version of chromium in combination with the newer selenium 4.0.
How to fix it
Option A — Software Update.
Make sure you have the latest version installed for your web browser, web driver and selenium.
Option B — Code Patch.
In case you can't update (I had the problem on my RaspberryPi, here I don't have the option to update Chromium since it is no longer supported.):
You have to activate the w3c option for your webdriver.
import time
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options # [!]
s = Service("/usr/local/bin/chromedriver")
opts = Options() # [!]
opts.add_experimental_option('w3c', True) # [!]
driver = webdriver.Chrome(service=s, options=opts) # [!]
driver.get("https://linkedin.com/login")
time.sleep(2)
username = driver.find_element(By.ID,"username")
username.send_keys("my email address goes here")

Related

Python Selenium sign in

So i want to compose a email to my another gmail account in selenium but they ask me to sign in to my google account i dont have any problem in signing in but can i automate that through a computer i tried to do it but i got a error always here is my code.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
PATH = "C:\\edge driver\\msedgedriver.exe"
driver = webdriver.Edge(PATH)
driver.get("https://mail.google.com/mail/u/0/#inbox")
before = driver.find_element_by_id("identifierId")
before.send_keys("arkodeep.ray#gmail.com")
before.send_keys(Keys.RETURN)
# link = driver.find_element_by_link_text("Compose")
So whenever i run this code i get a error by google saying This browser or app may not be secure. Learn more Can anyone help me how to fix that i want to know how to fix that in selenium using a computer bot in msedge. Can someone help?
Here is the error i am getting
There are several ways to resolve this issue:
Use Firefox instead of Chrome
from selenium import webdriver
driver = webdriver.Firefox()
Try to enable less secure apps to access your account.
You can find the option here:
https://myaccount.google.com/lesssecureapps

Python Selenium - FedEx login not working

I want to automate the generation of shipping labels through FedEx, as we need to type 50+ at a time.
However, it looks like FedEx is blocking me from logging in using selenium but I can't quite tell if it's the website or my code.
I am pretty new to both python and selenium.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
email = "my_email"
pwd = "my_password"
chrome_driver_path = "/Users/siddhartha/Developer/chromedriver"
driver = webdriver.Chrome(executable_path=chrome_driver_path)
driver.get("https://www.fedex.com/en-us/home.html")
location_click = driver.find_element_by_xpath('/html/body/div[4]/div/div/div/div/div/div/div[2]/ul/li[1]/a/span')
location_click.click()
signup = driver.find_element_by_xpath('/html/body/div[1]/header/div/div/nav/div/div/div/div[1]/a/span')
signup.click()
user_id_field = driver.find_element_by_id("NavLoginUserId")
user_id_field.send_keys(email)
time.sleep(4)
pwd_id_field = driver.find_element_by_id("NavLoginPassword")
pwd_id_field.send_keys(pwd)
time.sleep(3)
log_in_button = driver.find_element_by_xpath('/html/body/div[1]/header/div/div/nav/div/div/div/div[1]/div/div/form/button')
log_in_button.click()
Everything works until it presses LogIn and this error happens:
I added the time.sleep thinking they were blocking me to sign in because of the fast email/password typing.
Just encountered the same situation. The article linked by #Eric Ballard turned out to be very helpful. For me it was enough to add only the first option from that article: options.add_argument('--disable-blink-features=AutomationControlled')
So my code is the following:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument('--disable-blink-features=AutomationControlled')
browser = webdriver.Chrome(options=options)
browser.get('https://www.fedex.com/fedexbillingonline/pages/accountsummary/accountSummaryFBO.xhtml')
I had the same issue and was never able to find a solution. Your code looks fine, it's just that FedEx does something to block the browser from logging in when run with Selenium.
Seems like an Imperva type of deal, like Robert stated FedEx is likely detecting the use of bots. This may be of some use: https://piprogramming.org/articles/How-to-make-Selenium-undetectable-and-stealth--7-Ways-to-hide-your-Bot-Automation-from-Detection-0000000017.html

Reopen same browser window using selenium python and Firefox

Hey i'm trying to make an automatic program to send Whatsapp messages.
I'm currently using python, Firefox and selenium to achieve that.
The problem is that every time i'm calling driver.get(url) it opens a new instance of the firefox browser, blank with no memories of the last run. It makes me scan the bar code every time I run it.
from selenium import webdriver
from selenium.webdriver.firefox.webdriver import FirefoxProfile
cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile")
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile)
driver.get('http://web.whatsapp.com')
#Scan the code before proceeding further
input('Enter anything after scanning QR code')
I've tried to use profile but it seems like it has no affect.
cp_profile = webdriver.FirefoxProfile("/Users/Hodai/AppData/Roaming/Mozilla/Firefox/Profiles/v27qat5d.whatsapp_profile")
driver = webdriver.Firefox(executable_path="/Users/Hodai/Desktop/geckodriver",firefox_profile=cp_profile)
At the end I used chromedriver to achive my goal.
I tried cookies with pickle but it was a bit tricky because it remembered the cookies just for the same domain.
So I used user data for chrome.
now it works like a charm. thank you all.
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_argument("user-data-dir=C:/Users/Designer1/AppData/Local/Google/Chrome/User Data/Profile 1")
driver = webdriver.Chrome(chrome_options=options,executable_path="C:\webdrivers\chromedriver.exe")
The easiest way I think is to save your cookies after scanned the qrcode and push them to Selenium manually.
# Load page to be able to set cookies
driver.get('http://web.whatsapp.com')
# Set saved cookies
cookies = {'name1': 'value1', 'name2', 'value2'}
for name in cookies:
driver.add_cookie({
'name': name,
'value': cookies[name],
})
# Load page using cookies
driver.get('http://web.whatsapp.com')
To get your cookies you can use the console (F12), Network tab, right click on the request, Copy => Copy Request Headers.
It should not be like that. It only opens the new window when initialized with new variable or the program starts again. Here is the code for chrome. It doesn't matter how many times you call driver.get(url) it would open the url in the same browser window
from selenium import webdriver
import selenium.webdriver.support.ui as ui
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
from selenium.webdriver.common.by import By
import time
driver = webdriver.Chrome(executable_path=r"C:\new\chromedriver.exe")
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
time.sleep(10)
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
time.sleep(10)
driver.get('https://www.olx.com.pk/lahore/apple/q-iphone-6s/?search%5Bfilter_float_price%3Afrom%5D=40000&search%5Bfilter_float_price%3Ato%5D=55000')
time.sleep(10)
Let me know if the issue is resolved or you are trying to do something else.

Issue with locαting HTML with Selenium

Trying to learn Selenium for Python (3.4.0) and have had success with the basic things - installing, opening browser and web page, so on. But when I try to open a specific HTML form I am met with an error - something to do with the 'driver' at the beginning of the 'driver.find_element_by_name'.
My code is:
#vocab express logger onner
import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.common.exceptions import NoSuchElementException
browser=webdriver.Firefox()
browser.get("https://www.vocabexpress.com/login/")
uname = driver.find_element_by_name("uname")
uname.send_keys("13holmee")
and the error message is:
uname = driver.find_element_by_name("uname")
NameError: name 'driver' is not defined
Sorry if this too simple a question or has been asked before (I couldn't find anything), I'm still new to this.
Thanks
There is no driver in your namespace, because you have not defined a variable with that name.
find_element_by_name is a method of the webdriver.Firefox object, which in this case you have named browser. Try uname = browser.find_element_by_name("uname").

Handling "Authentication Required" alert box with Python 2.7 + Selenium Webdriver

I am having an issue with a secure URL:
Opening the URL creates an "Authentication Required" alert box with username and password fields.
I am fairly new to Selenium Webdriver and Python. I am not familiar with handling alerts and am currently manually typing in credentials until I can get this figured out. I have already tried adding my username/password into the URL. This does not work for me.
Could someone please point me in the direction of entering keys into username and password fields in an alertbox?
Could you try using Keys to tab within the alert?
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
from selenium.webdriver.support.ui import WebDriverWait as wait
from selenium.webdriver.support import expected_conditions as EC
driver = webdriver.Firefox()
driver.get('http://www.url.com/')
wait(driver, 5).until(EC.alert_is_present())
alert = driver.switch_to_alert()
alert.send_keys('username')
alert.send_keys(Keys.TAB)
alert.send_keys('password')
alert.accept()
In case of such authentication, you need pass username and password to server while accessing page to avoid authentication window(which is out of reach of selenium)
Suppose the url you're trying to access is: http://example.com
you'll have to access this url with credentials like following:
driver.get('http://username:password#example.com')
where username is your username and password is your password for the site.
Thanks for all of the responses. Unfortunately, none of these solutions worked for me. I suspect it may have something to do with the creation of a new profile every time firefox was opened by webdriver.
My workaround:
I changed the driver from Firefox to IE, after installing the 32bit IE driver(http://selenium-release.storage.googleapis.com/index.html?path=2.44/). This solved my issue by no longer creating the alertbox, and allowing me to continue with my unittest.
I was having similar issues where adding my username/password into the URL did not work for me. This was because Firefox was alerting me with a confirmation box requiring me to confirm that I wanted to log into the site with the username provided. The following solved this issue:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://<username>:<password>#<site-needing-auth>.com')
alert = driver.switch_to_alert()
alert.accept()
None of the answer before helped with my situation. The website I am authenticating to uses single sign on which was posing issues when using username:password#website.com.
In the Authentication window two fields needed to be entered both User Name and Password.
To solve this send the user name and password at one time to the alert box.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.alert import Alert
def login(self):
self.browser = webdriver.Firefox()
self.browser.get(r'websitelogin.com')
wait(self.browser, 1).until(expected_conditions.alert_is_present())
# "Switch" to the Alert browser
alert = Alert(self.browser)
# Send the username, TAB then password all in one go using a python f string
alert.send_keys(f'username{Keys.TAB}password')
alert.accept()
I was having the exact same problems as you until I noticed, that I simply forgot to write: 'https' instead of just http. If you add the 's', for me that did it!
So in code maybe you want to try:
driver.get('https://username:password#domain-name.org')
The below Python code can help to load such a URL prompting for authentication within a JavaScript Popup alert, I was also stuck here for a long time. It's because of Chrome driver will not allow such authentication techniques after the update 59 (probably). There are still backdoors via Selenium using the JavaScript engine in the browser to load such URLs.
driver.get("https://www.google.com")
URL = "https://{username}:{password}#www.example.com"
driver.executeScript("window.open('"+URL+"')")

Categories