1

I'm using Selenium in Python to click a text entry field and write some text into it. Neither the .click() nor .send_keys() methods are being recognized. Can someone help with this?

Also, is there a way to stop Selenium from printing to the console automatically? My program is console-based and Selenium is writing things to an input() that I gave because it prints to the console.

Here is a code snippet:

url = "https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65"
browser = webdriver.Edge("Web Scrapers\msedgedriver.exe")
browser.get(url)
textbox = browser.find_element_by_id("LocationSearch_input")
textbox.click()
textbox.send_keys(zipcode)
textbox.send_keys(Keys.ENTER)
6
  • 2
    Can you please provide minimal reproducible example to see what you had done? Commented Jul 20, 2021 at 19:59
  • What is the error/issue you are getting? Commented Jul 20, 2021 at 20:29
  • you need to wait for it to show up Commented Jul 20, 2021 at 20:47
  • @JeremyKahan it's rendered fairly immediately, he's trying to grab a hidden element is the problem Commented Jul 20, 2021 at 20:47
  • oh, okay, I was just about to try it. I am glad I did not make that an answer Commented Jul 20, 2021 at 20:48

4 Answers 4

1

you could try the explicitWait hope this will work for you

WebDriverWait(browser,10).until(EC.element_to_be_clickable((By.XPATH,"//input[@type='text']"))).send_keys("20874",Keys.RETURN)
Sign up to request clarification or add additional context in comments.

Comments

0

I would suggest you to do it with explicit wait.

I am giving this answer, cause none of the answer's are really using Explicit waits with ID attribute :

driver.maximize_window()
driver.implicitly_wait(30)
driver.get("https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65")
wait = WebDriverWait(driver, 10)
wait.until(EC.element_to_be_clickable((By.ID, "LocationSearch_input"))).send_keys('60007' + Keys.RETURN)
wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "div[class^='CurrentConditions--dataWrapperInner']"))).click() 

Imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Comments

0

From what I can tell, everything is working fine here, however you are pointing your textbox variable to the wrong HTML element.

LocationSearch_input is a hidden label that isn't directly attached to the searchbox. I would try pointing it to

SearchInput--SearchInput--M7lKl SearchInput--enableSearchIcon--1Ugsx, one of the parent elements.

6 Comments

The .click(), and .send_keys() methods still aren't recognized, though, which was the problem.
Please elaborate on "aren't recognised", are you getting an error in your output? Or is it just not clicking.
Well, in Visual Studio Code it doesn't show that the methods are there, since they are in white.
Are you importing selenium at the top? If so, don't rely on your code editor to tell you what exists and what doesn't, always use documentation. Don't focus on what your editor shows, what matters is: does the code run? And what happens?
ElementNotInteractableException: Element <div class="SearchInput--SearchInput--M7lKl SearchInput--enableSearchIcon--1Ugsx"> is not reachable by keyboard
|
0

At least testing in firefox, it's a timing thing. The click works. The error of element not interactable (not reachable by keyboard) comes off of the send_keys line. If we wait after the click(), the element becomes reachable by keyboard.

The following could probably be refined (really don't like sleep, but sometimes it works), but works for me:

url = "https://weather.com/weather/today/l/69ef4b6e85ca2de422bea7adf090b06c1516c53e3c4302a01b00ba763d49be65"
browser.get(url)
browser.find_element_by_id('LocationSearch_input').click()
time.sleep(5)
browser.find_element_by_id('LocationSearch_input').send_keys('Boston')

At that point you need to click on whichever of the 10 options is what you really want.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.