0
from selenium import webdriver
import time
from selenium.webdriver.common.keys import Keys

driver = webdriver.Chrome(r"C:\Users\m\Desktop\python\selen\chromedriver.exe")
driver.set_page_load_timeout(10)
driver.get("https://google.com")
driver.find_element_by_name("q").send_keys("Automation")
driver.find_element_by_name("btnk").send_keys(keys.ENTER)
time.sleep(4)
driver.quit()

I am trying this simple code to open google page and search "automation" word, but i get this result and the program stops running at this line: driver.find_element_by_name("btnk").send_keys(keys.ENTER)

Traceback (most recent call last):
  File "C:\Users\m\Desktop\python\selen\auto.py", line 9, in <module>
    driver.find_element_by_name("btnk").send_keys(keys.ENTER)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 496, in find_element_by_name
    return self.find_element(by=By.NAME, value=name)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 978, in find_element
    'value': value})['value']
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\ProgramData\Anaconda3\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"btnk"}
  (Session info: chrome=74.0.3729.131)
  (Driver info: chromedriver=74.0.3729.6 (255758eccf3d244491b8a1317aa76e1ce10d57e9-refs/branch-heads/3729@{#29}),platform=Windows NT 10.0.17134 x86_64)

What is the problem?

Hasani
  • 3,543
  • 14
  • 65
  • 125
  • If you could share the html for the element you're trying to send keys to that would help (I could do that busy work... but what if the site changes for future readers?). With your exception, most likely there is no element with the `name` attribute set to `btnk`. It could also be a waiting issue where that line is called before the element appears in the DOM. – mrfreester May 10 '19 at 14:30

2 Answers2

1

Please check 'find_element_by_name' for search button or use xpath.

I run script in java. Hope this helps you...

And for finding xpath use selenium IDE.

Selenium IDE

and code is below:-

WebDriver driver =new FirefoxDriver();
driver.get("https://www.google.com");

WebDriverWait wait = new WebDriverWait (driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='q']")));

WebElement ele = driver.findElement(By.xpath("//input[@name='q']"));
ele.sendKeys("q");
driver.findElement(By.xpath("(//input[@name='btnK'])[2]")).sendKeys(Keys.ENTER);

And one suggestion drop 'thread.sleep' , because it stop program for mention time.

Thread.sleep(): In sleep code will always wait for mentioned seconds in side the parentheses even in case working page is ready after 1 sec. So this can slow the tests.

Explicit wait: An explicit waits is a kind of wait for a certain condition to occur before proceeding further in the code.

Implicit wait: An implicit wait is to tell WebDriver to poll the DOM for a certain amount of time when trying to find an element or elements if they are not immediately available. The default setting is 0

Is it good practise to use Thread.sleep while doing selenium Test?

Edited:

also refer this https://stackoverflow.com/a/56078618/4513879 answer.

Element is not reachable by keyboard in plain words means that the element can’t be reached using the keyboard, which means you won't be physically interact with it even.avoid using JS clicks. A user can't click an element that isn't visible or is covered or is off the screen, etc. Selenium was designed to act like a user and throw errors when a user can't click an element.

For more information please go through https://stackoverflow.com/a/49872160/4513879 this answer.

Updated Code:-

WebDriver driver =new FirefoxDriver();
driver.get("https://www.google.com");

WebDriverWait wait = new WebDriverWait (driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@name='q']")));

WebElement ele = driver.findElement(By.xpath("//input[@name='q']"));
ele.sendKeys("q");

new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("btnK"))).click();

in above code I added new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.name("btnK"))).click(); this line.

Pradnya Bolli
  • 1,915
  • 1
  • 19
  • 37
1

K should be capital. It is case sensitive

Change this:

driver.find_element_by_name("btnk").send_keys(keys.ENTER)

To this:

driver.find_element_by_name("btnK").send_keys(keys.ENTER)

Some other observations... There appear to be two elements with the exact same attributes, so you may still run into issues with this.

As a possible workaround for that issue, you could try this instead of find_element_by_name:

from selenium.webdriver.support import expected_conditions as EC

EC.element_to_be_clickable(By.name("btnK"))).click()

Also, with buttons I would think you would want to click, instead of send_keys

Cheers :)

mrfreester
  • 1,981
  • 2
  • 17
  • 36
  • but its gives error `'Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: Element is not visible'` – Pradnya Bolli May 10 '19 at 16:25
  • Yeah... you're running into the issue where you're getting the duplicate element :( I'll add something using expected conditions as a possible workaround... Not sure why they made both of those exactly the same. – mrfreester May 10 '19 at 17:27
  • 1
    I've updated the answer to explicitly filter out elements that aren't clickable – mrfreester May 10 '19 at 19:56