Can't send keys selenium webdriver python - python

Trying to execute simple test
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey')
And getting the error
Traceback (most recent call last):
File "C:/webdriver/test.py", line 8, in <module>
driver.send_keys('hey')
AttributeError: 'WebDriver' object has no attribute 'send_keys'
What's the problem?

WebDriver instance does not have send_keys() method. That's what the error is actually about:
'WebDriver' object has no attribute 'send_keys'
Call send_keys() on a WebElement instance which is returned by find_element_by_*() methods - find_element_by_name() in your case:
element = driver.find_element_by_name('q')
element.send_keys("hey")
And just FYI, there is also an ActionChains class which is useful do build up chains of actions or apply more complex actions like drag&drop or mouse move. It's an overhead in this case, but just for the sake of an example:
from selenium.webdriver.common.action_chains import ActionChains
actions = ActionChains(driver)
actions.move_to_element(element).send_keys("hey").perform()

You must change the reference element
driver.get('http://google.com')
elem.find_element_by_name('q')
elem.send_keys('hey')

Did you try changing your reference element.
It would be a good practice if you call webdriver using different reference.
Your code after changing reference.
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Firefox()
driver.get('http://google.com')
#Firefox Webdriver is created and navigated to google.
elem = driver.find_element_by_name('q')
elem.send_keys('hey',Keys.RETURN)
#Keys.RETURN = Pressing Enter key on keyboard
time.sleep(5)
driver.close()

I had faced same issue, but I got solution for that.
Use move_to_element in ActionChains.
Find element. elem = driver.find_element_by_*
create your actionchain driver actions = ActionChains(driver)
use move to command, so webdriver will point to that element's position, but before you send key, you need to set webdriver position on that element by using click() function. Now, webdriver has a surface area to place given keys (data) and for that you will use send_keys() function. In the last just put perform() function to execute these all task very smoothly.
actions.move_to_element(<ELEMENT>).click().send_keys(<DATA>).perform()
https://selenium-python.readthedocs.io/api.htmlt
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
elem = driver.find_element_by_name('q')
actions = ActionChains(driver)
actions.move_to_element(elem).click().send_keys('hey').perform()

The problem with your code is you have not tell driver where to send the keys
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q')
driver.send_keys('hey') // driver dont know where to send the keys
correct code would be :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
goog = driver.find_element_by_name('q') // you have stored element in goog variable
goog.send_keys('hey') // you told variable to send hey
or
you can directly send after finding the element like :
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
driver = webdriver.Firefox()
driver.get('http://google.com')
driver.find_element_by_name('q').send_keys('hey')
If it helps so please accept the answer !!

Its with the version of selenium which is causing the issue. I faced the same issue.
Its with the selenium version 3.3.3 which has the compatibility problem.
Try:
pip uninstall selenium
pip install selenium==3.3.1
Hope it works.

Related

Selenium does't open firefox

I'm trying use selenium and firefox, but it's just open the browser and set this error:
Error
With Chrome is ok.
This is the code:
from selenium import webdriver
driver = webdriver.Firefox(executable_path = "c:/my-apps/geckodriver.exe")
Thanks very much
When you try to find any element in an HTML page that does not exist, NoSuchElementException will be raised.
First Possible Solution:
You need to import
from selenium.common.exceptions import NoSuchElementException
Then you can use try except block
try:
your_element = driver.find_element_by_xpath(".//*[#id='loginForm:username']")
your_element.click()
except NoSuchElementException:
pass
Second Possible Solution:
Without importing anything, checking whether that element exist, if it does then it will be clicked
your_element = driver.find_elements_by_xpath(".//*[#id='loginForm:username']")
if len(your_element) > 0:
elem[0].click()
HEY You can use this code as given below
driver = webdriver.Firefox()
driver.implicitly_wait(5)
driver.maximize_window()
driver.get('URL')
I think you should put your geckodriver in the same folder where you are coding
and if this does not work you need to upgrade geckodriver
HEY According to your ERROR
when you open the website your 1 element is not correct
you need to change your 1 element of your website
wait=WebDriverWait(self.driver,60)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR,"#loginForm:username"))).send_keys("a")
Generally Firefox is a tad slower than Chrome so your issue may be timing for find elements. What is recommended is waiting for the element to be clickable and then proceeding to send keys to it.
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

How can I make the selenium chromedriver make wait until it maximizes the window?

I've been messing around with python/selenium a bit, now I got to use the "try" method. I want the driver to wait until it maximized the window, and then go on with the code. At the beginning it just went on executing all scripts and the chrome window got maximized whilst executing it, which looked pretty weird.
Anyways I hope you can help me,
Have a good day y'all ;))
Ah, here's the code: (And the error message beneath it)
from selenium import webdriver
import time
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
driver = webdriver.Chrome(executable_path=r'C:\Users\Anyone\Desktop\Python\chromedriver.exe')
print("Automation Started, please hold on.")
driver.get("https://blablabla")
try:
recap = WebDriverWait(driver, 5).until(
EC.WebDriverException(driver.maximize_window())
)
finally:
driver.forward()
And here's the error message:
Traceback (most recent call last):
File "C:/Users/Anyone/PycharmProjects/blebleble/blablabla.py", line 17, in <module>
EC.WebDriverException(driver.maximize_window())
File "C:\Users\Anyone\PycharmProjects\blablabla\venv\lib\site-packages\selenium\webdriver\support\wait.py", line 71, in until
value = method(self._driver)
TypeError: 'WebDriverException' object is not callable
Don't wait for the Selenium driven ChromeDriver initiated google-chrome Browsing Context to open first and then maximize it.
Instead, add the argument start-maximized and configure the driver, so the Google Chrome Browsing Context opens up maximized using the solution below:
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument("start-maximized")
driver = webdriver.Chrome(options=options, executable_path=r'C:\WebDrivers\chromedriver.exe')
driver.get("https://blablabla")
After making driver object use the below code:-
driver.manage().window().maximize();
and remove the try/finally block.

How do I fill out a form using Python and Selenium?

I am new to Python and wanted to use it for automatic login. I found https://automatetheboringstuff.com/chapter11/ and tried:
#! python3
from selenium import webdriver
browser = webdriver.Firefox()
type(browser)
browser.get('https://forum-studienstiftung.de/')
emailEl = browser.find_element_by_id(username)
Unfortunately, this leads to:
Traceback (most recent call last): File "", line 1, in
emailEl = browser.find_element_by_id(username) NameError:
name 'username' is not defined
According to the Firefox Developer Tools the correct ID is "username".
Wrap username in quotation marks. Right now you are passing in a variable called username which selenium is trying to match with an id on the page with the same value. Since the value is none, Selenium cannot find it hence the error.
The page you are trying to access takes time to load. You have to wait for the element to be visible before accessing it.
Try this:
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
browser = webdriver.Firefox()
type(browser)
browser.get('https://forum-studienstiftung.de/')
emailEl = WebDriverWait(driver, 20).until(EC.presence_of_element_located((By.id, "username")))

Pressing ctrl+t doesn't work in Selenium Webdriver using ActionChains

I need to open a new browser tab in my test and I've read that the best approach is to simply send the appropriate keys to the browser. I'm using windows so I use ActionChains(driver).send_keys(Keys.CONTROL, "t").perform(), however, this does nothing.
I tried the following to test that Keys.CONTROL is working properly:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "v").perform()
This indeed passes whatever I have copied in the clipboard to the Google search box that is in focus by default.
This is what I want to use but that is not working:
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()
Nothing seems to happen to the browser, no new tab opened, no dialog box, no notification. Does anyone know why this is?
Try this java Script Executor it should work.
link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
Edited code with window handle.
driver=webdriver.Chrome()
driver.get("https://www.google.com")
window_before = driver.window_handles[0]
link="https://www.google.com"
driver.execute_script("window.open('{}');".format(link))
window_after = driver.window_handles[1]
driver.switch_to.window(window_after)
driver.find_element_by_name("q").send_keys("test")
try executing this script:
driver.execute_script("window.open('https://www.google.com');")
for example
myURL = 'https://www.google.com'
driver.execute_script("window.open('" + myURL + "');")
You’ve gotten some good answers utilizing JavaScript execution, but I am curious why your example doesn’t work in the first place.
It’s possible that your ActionChains line is executed before the page has fully loaded; you could try adding a wait as follows:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by
from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.keys import Keys
def test_trial():
driver = webdriver.Chrome()
driver.get("https://www.google.com/")
try:
element = WebDriverWait(driver, 10).until(
EC.presence_of_element_located(By.TAG_NAME("body")))
ActionChains(driver).send_keys(Keys.CONTROL, "t").perform()

Unable to submit keys using selenium with python

Following is the code which im trying to run
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import os
import time
#Create a new firefox session
browser=webdriver.Firefox()
browser.maximize_window()
#navigate to app's homepage
browser.get('http://demo.magentocommerce.com/')
#get searchbox and clear and enter details.
browser.find_element_by_css_selector("a[href='/search']").click()
search=browser.find_element_by_class_name('search-input')
search.click()
time.sleep(5)
search.click()
search.send_keys('phones'+Keys.RETURN)
However, im unable to submit the phones using send_keys.
Am i going wrong somewhere?
Secondly is it possible to always use x-path to locate an element and not rely on id/class/css-selections etc ?
The input element you are interested in has the search_query class name. To make it work without using hardcoded time.sleep() delays, use an Explicit Wait and wait for the search input element to be visible before sending keys to it. Working 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
browser = webdriver.Firefox()
browser.maximize_window()
wait = WebDriverWait(browser, 10)
browser.get('http://demo.magentocommerce.com/')
browser.find_element_by_css_selector("a[href='/search']").click()
search = wait.until(EC.visibility_of_element_located((By.CLASS_NAME, "search-query")))
search.send_keys("phones" + Keys.RETURN)

Categories