Python selenium - AttributeError: 'dict' object has no attribute 'get_attribute' - python

I wanted to showcase selenium and instead of starting a complete Java / C# suite I wanted to quickly use Python since it can be done with a single script.
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
print("sample test case started")
driver = webdriver.Chrome()
driver.maximize_window()
driver.get("MyUrl")
element = driver.find_element(By.ID, "Username")
element.send_keys("MyUsername")
driver.close()
print("sample test case successfully completed")
I am getting this error message:
AttributeError: 'dict' object has no attribute 'send_keys'
I would seam that find_element method is returning some kind of dict (Dictonary??) object instead of a selenium web element. How can I fix this??

I ran into this exact issue. It seems to be some sort of bug in Selenium 4.0.
I have reverted to 3.141.0 (pip install selenium==3.141.0) and it seems to have resolved this issue.

Selenium project is moving to only support W3C and Chrome (since version 75) is using W3C by default. You can run into an issue that you described, if you have old version of chromedriver and selenium >=4, which was the problem in my case. Update your chromedriver or downgrade Selenium version to <4 and it should fix your problem.
I reproduced this problem using Selenium 4.1.0 and Chromedriver 98, by disabling w3c:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
options = Options()
options.add_experimental_option("w3c", False)
driver = webdriver.Chrome(options=options)
So, it's another option that could lead to this error.
You can fix it by removing it or changing its value to True.

Related

How do I avoid a file path error while importing ChromeDriver?

I have been trying to work on my first web-scraping project for which I am using Selenium. However, I seem to be running into some issues with importing the ChromeDriver. I am using Selenium 3.0.0 and am working on Chrome.
webdriver_service = Service(ChromeDriverManager().install())
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')
I keep getting the following message: 'chromedriver.exe' executable needs to be in PATH.
Let me know if there's some issue with the file path I am using as I think that is where the issue is coming from.
As answer tells, you should specify the chromedriver-path as following:
driver = webdriver.Chrome('/Users/MyUsername/Downloads/chromedriver.exe')
The executable_path= argument stands for chrome.exe, meaning google-chrome browser.
This should be done through the OS when possible and not through code as it needs to be done for every project this way but it is possible to set environment variables through code
import os
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
# Setting said environment variable
os.environ["CHROME_DRIVER_PATH"] = "Z:\\port\\driver\\chromedriver"
# Reading variable we created
DRIVER_PATH = os.environ.get('CHROME_DRIVER_PATH')
s=Service(DRIVER_PATH)
Got my answer from here
I'm going to be honest and say the path looks good to me. I even tried to run it in Visual Studio and it worked perfectly, so I have no idea why it isn't working for you. That being said I do have a way you can fix it. You can just not use executable_path. You already imported webdriver whose sole purpose is to handle the webdriver for selenium for you.
See here.
You don't even have to change much.
#pip install webdriver-manager, pip install selenium==3.0.0
import os
import logging
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
chrome_options = Options()
chrome_options.add_argument("--headless") # Ensure GUI is off
chrome_options.add_argument("--no-sandbox")
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
driver = webdriver.Chrome(ChromeDriverManager().install())
#driver = webdriver.Chrome(executable_path='/Users/MyUsername/Downloads/chromedriver.exe')
driver.get('https://www.google.com/')
Hope this helps.
You have to take care of a couple of things:
As you are using Selenium 3.0.0 you don't have to create any Service() object. So you can remove the line:
webdriver_service = Service(ChromeDriverManager().install())
Incase you don't intend to use ChromeDriverManager() you can remove the following lines:
# Silent download of drivers
logging.getLogger('WDM').setLevel(logging.NOTSET)
os.environ['WDM_LOG'] = 'False'
Unless you are executing your test in headless mode you won't require the argument --headless. So you can remove the line:
chrome_options.add_argument("--headless") # Ensure GUI is off
Unless you are executing your test as a root/administrator you won't nee the argument --no-sandbox. So you can remove the line:
chrome_options.add_argument("--no-sandbox")
Now you can download the matching ChromeDriver version from ChromeDriver page, unzip/untar it and use the following lines of code:
from selenium import webdriver
driver = webdriver.Chrome(executable_path='/path/to/chromedriver') # for windows OS use chromedriver.exe
driver.get('https://www.google.com/')

Selenium Firefox profile.add_extension not working. Script running fine but I don't see any extension

from selenium import webdriver
from selenium.webdriver import FirefoxProfile
driver = webdriver.Firefox(
executable_path=r'C:\Users\subha\Downloads\geckodriver-v0.30.0-win64\geckodriver.exe')
profile = FirefoxProfile(r'C:\Users\subha\AppData\Roaming\\Mozilla\Firefox\Profiles\e7j8p01g.F1')
profile.add_extension(r'C:\Users\subha\AppData\Roaming\Mozilla\Firefox\Profiles\e7j8p01g.F1\extensions\CanvasBlocker#kkapsner.de.xpi')
driver.get('https://github.com')
This is a possible duplicate from previous question. A more elegant solution than the workaround proposed there is the following option from the official documentation which works as expected:
driver = webdriver.Firefox('path/to/executable')
driver.install_addon('~/path/to/addon.xpi')
In your case do:
from selenium import webdriver
driver = webdriver.Firefox(executable_path=r'C:\Users\subha\Downloads\geckodriver-v0.30.0-win64\geckodriver.exe')
driver.install_addon(r'C:\Users\subha\AppData\Roaming\Mozilla\Firefox\Profiles\e7j8p01g.F1\extensions\CanvasBlocker#kkapsner.de.xpi')
#Maybe time.sleep() a few seconds to give time for the installation, then
driver.get('https://github.com')

Why is my Selenium selector pulling a dict? (Django/Python 3.8)

I've just updated selenium in my django app on PythonAnywhere. This is my code:
from selenium import webdriver
def Synonym():
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--disable-gpu")
browser = webdriver.Chrome(options=chrome_options)
browser.get("https://www.synonym.com/synonyms/test")
test = browser.find_element_by_class_name("logo").text
browser.quit()
return test
But it gives me an error:
AttributeError: 'dict' object has no attribute 'text'
When I grab it without the .text this is what it gives me:
{'ELEMENT': '0.3567871003333163-1'}
I'm using a paid account and should be able to access any site. Sidenote: is there a way to stop selenium from making /tmp/ files and folders?
Thanks!
The fix for me on PythonAnywhere was to update the system image:
https://help.pythonanywhere.com/pages/ChangingSystemImage
Answer courtesy of caseneuve!

Using python selenium for Microsoft edge

I am trying to use pythons selenium for Microsoft edge but I keep getting this error:
WebDriverException: Message: unknown error: cannot find Microsoft Edge binary
I downloaded the latest version of the edge driver. Here is my code:
from selenium import webdriver
from selenium.webdriver.remote import webelement
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support import expected_conditions as EC
import pandas as pd
import time
from bs4 import BeautifulSoup
import os
from datetime import datetime
from selenium.webdriver import ActionChains
driver = webdriver.Edge(executable_path = 'C:\\Users\\Downloads\\edgedriver_win32\\msedgedriver.exe')
def get_trulia_estimate(address):
driver.get('https://www.trulia.com/')
print(address)
element = (By.ID, 'homepageSearchBoxTextInput')
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(element)).click()
WebDriverWait(driver, 10).until(EC.element_to_be_clickable(element)).send_keys(address)
search_button = (By.CSS_SELECTOR, "button[data-auto-test-id='searchButton']")
WebDriverWait(driver, 50).until(EC.element_to_be_clickable(search_button)).click()
time.sleep(3)
This post is quite old now, but hopefully I can help anyone that stumbles upon the same issue in future!
The problem is that you're using the wrong webdriver. Edge exists in two different versions, implemented on two non-interchangeable engines -- Chromium Edge and EdgeHTML (the default version at the time of writing). Each of these two versions has a different webdriver associated with it, with Chromium Edge's being "msedgedriver.exe", and EdgeHTML's being "MicrosoftWebDriver.exe".
You are using the EdgeHTML version of Edge, while trying to run the Chromium Edge webdriver. The 'cannot find Microsoft Edge binary' error Selenium spits out comes from this.
Luckily it is easy to install the right webdriver. If you have a Edge 17 or older, you can install the driver here. Make sure you download the EdgeHTML driver, not the Chromium driver, and add it to your PATH. For Edge 18 and later, you don't have to download anything. Simply run in the command prompt the command: DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0.
WebDriver cannot find your MS Edge path, u can try to uninstall and reinstall Edge.
If its not gonna help add Edge location to your system path or use --binary argument.
The Answer by James L is perfectly summarized. I have Microsoft EdgeHTML 18.17763 and I tried to, therefore, run the command:
DISM.exe /Online /Add-Capability /CapabilityName:Microsoft.WebDriver~~~~0.0.1.0
This executed successfully. However, when running my code this time around, I get the error:
Message=A exception with a null response was thrown sending an HTTP
request to the remote WebDriver server for URL
http://localhost:52109/session. The status of the exception was
ReceiveFailure, and the message was: The underlying connection was
closed: An unexpected error occurred on a receive.
Looks like we need to, additionally, also enable developer options in Windows>>Settings>>Developer Options, which, since I do not have admin privileges, I am currently unable to do.
You must install msedge driver as You do with Chromedriver.
You first check wich version your edge is and then download de Edge driver from Microsoft Page
Download and unzip the msedgedriver file according your browser version
Install the package of it to python in terminal (pip install msedge-selenium-tools selenium==3.141)
Finally try the code to get the browser:
from selenium import webdriver
edgeBrowser = webdriver.Edge(r"C:....\msedgedriver.exe")
edgeBrowser.get('https://www.google.com')

'window_maximize()' is not an attribute of WebDriver in Selenium

I am using Selenium and for this task I need to maximize the browser after the page is loaded, the problem is that I am getting the following error and can't seem to understand how to solve it.
AttributeError: 'WebDriver' object has no attribute 'window_maximize'
Here is the code I am testing
from pyvirtualdisplay import Display
from ftplib import FTP
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.common.keys import Keys
#initialize HIDDEN display
display = Display(visible=0, size=(1366, 768))
display.start()
browser = webdriver.Firefox()
browser.get('http://youtube.com/')
browser.window_maximize();
...
Isn't window_maximize an attribute of the browser?
I am using python and Selenium Server 2.28
Any tip much appreciated!
You can use browser.maximize_window(). I think the problem was in the wrong function name.
OK, after much looking for how to use window_maximize I found out I can use browser.set_window_size(800, 600) instead.
I tested and it works fine. It is important to set the browser to browser.set_window_position(0, 0)
The answer was found here How to maximize a browser window using the Python bindings for Selenium 2-WebDriver?

Categories