Issue with locαting HTML with Selenium - python

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").

Related

Why am I getting the message that the search-bar is not intractable?

The code is supposed to type "fish" into the YouTube search bar using Selenium and a Chrome Browser.
I have tried the xpaths of mulitple divs that hold the tag and they didn't work either.(not sure if the error was the same though) The xpath in the code is for the <input> tag so it should be fine.
I also watched a tutorial and the xpath was exactly the same so that shouldn't be the problem since it worked for the YouTuber.
It also took me some time to figure out that the find_element_by_* are depreciated functions.
Could it be that the .send_keys has also been changed? I did try to find the selenium changes in 4.1.0 and it said nothing about it on a website that I found.
Should I maybe delete Selenium 4.1.0 and install an older version? For simplicity sake. Since there is probably a bigger number of tutorials for it.
from selenium import webdriver
from selenium.webdriver.common.by import By
setting = webdriver.ChromeOptions()
setting.add_argument("--incognito")
# I open the browser in incognito just so I don't clutter my search
# history with dumb stuff as I'm testing things out
# could it be a part of the problem?
driver = webdriver.Chrome(options = setting)
driver.get('http://youtube.com')
searchbox = driver.find_element(By.XPATH, '//*[#id="search"]')
searchbox.send_keys('fish')
Error Message:
selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
wait=WebDriverWait(driver,60)
driver.get('http://youtube.com')
searchbox = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"input#search")))
searchbox.send_keys('fish')
In order to send_keys to that element wait for it to interactable and then send keys.
Imports:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
Outputs:

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

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

Selenium get() redirects to another url

I'm trying to navigate to the following page and extract the html https://www.automobile.it/annunci?b=data&d=DESC, but everytime i call the get() method it looks like the website redirects me to the another page, always the same one which is https://www.automobile.it/torrenova?radius=100&b=data&d=DESC.
here's the simple code i'm running:
from selenium import webdriver
driver = webdriver.Chrome(executable_path=ex_path)
driver.get("https://www.automobile.it/annunci?b=data&d=DESC")
html=driver.page_source
if i do the same thing using the request module i don't get redirected
import requests
html=requests.get("https://www.automobile.it/annunci?b=data&d=DESC")
i don't understand why it's behaving like this, any ideas?
Use driver.delete_all_cookies()
from selenium import webdriver
driver = webdriver.Chrome(executable_path=ex_path)
driver.delete_all_cookies()
driver.get("https://www.automobile.it/annunci?b=data&d=DESC")
html=driver.page_source
PS: be also warned: Page_source will not get you the completed DOM as rendered.
Well you can clear browser cache by using the below code :
I am assuming that you are using chrome.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome(executable_path=ex_path)
driver.get('chrome://settings/clearBrowserData')
driver.find_element_by_xpath('//settings-ui').send_keys(Keys.ENTER)
driver.get("https://www.automobile.it/annunci?b=data&d=DESC")

Python, error with web driver (Selenium)

import time
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driver = webdriver.Chrome()
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)
I am getting the error:
FileNotFoundError: [Errno 2] No such file or directory: 'chromedriver'
I am not sure whythis is happening, because I imported selenium already.
Either you provide the ChromeDriver path in webdriver.Chrome or provide the path variable
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
driverLocation = 'D:\Drivers\chromedriver.exe' #if windows
driver = webdriver.Chrome(driverLocation)
driver.get('http://arithmetic.zetamac.com/game?key=a7220a92')
element = driver.find_element_by_link_text('problem')
print(element)
Best way to eliminate this Exception without altering the code eevn single line is to add the chromedriver.exe( or nay other browser driver files) in to Python
site_packages/scripts directory for windows
dist_package/scripts for Linux
Please check this solution, it works.
If you are using a Mac, then don't include '.exe' I put the selenium package directly into my Pycharm project that I called 'SpeechRecognition'. Then in the selenium file, navigate to: /selenium/webdriver/chrome, then copy and paste the 'chromedriver.exe' file you downloaded most likely from [here][1]
Try this script if you are using PyCharm IDE or similar. This should open a new Google window for you.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Chrome('/Users/Name/PycharmProjects/SpeechRecognition/selenium/webdriver/chrome/chromedriver')
browser.get('http://www.google.com')
Then if you want to automatically search an item on Google, add these lines below and run. You should see an automatic google search window opening up. It might disappear quickly but to stop that, you can simply add a while loop if you want or a timer
search = browser.find_element_by_name('q')
search.send_keys('How do I search an item on Google?')
search.send_keys(Keys.RETURN)
[1]: https://sites.google.com/a/chromium.org/chromedriver/home

'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