1

I have a very simple program that opens https://google.com and clicks on the first link. I have used WebDriverWait to make sure the element is ready to be clicked, though it still doesn't work, and outputs and error.

Code:

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
import time

wd = webdriver.Firefox()

wd.get("https://www.google.com/search?q=python")

# wd.find_element_by_css_selector("a").click()  # This doesn't work, same error
WebDriverWait(wd, 1000000).until(
    expected_conditions.element_to_be_clickable((By.CSS_SELECTOR, "a"))
).click()

time.sleep(5)

wd.close()

Error:

Traceback (most recent call last):
  File "c:\Users\ketha\OneDrive\Documents\Coding\Youngwonks\InteractingWithWebsites\selenium_basics.py", line 17, in <module>
    WebDriverWait(wd, 1000000).until(
  File "C:\Users\ketha\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 80, in click
    self._execute(Command.CLICK_ELEMENT)
  File "C:\Users\ketha\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
    return self._parent.execute(command, params)
  File "C:\Users\ketha\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "C:\Users\ketha\AppData\Local\Programs\Python\Python39\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotInteractableException: Message: Element <a class="gyPpGe"> could not be scrolled into view

I am using Firefox.
Firefox Version: 89.0 (64 bit)
Selenium Version: 3.141.0
Geckodriver Version: 0.29.1

1 Answer 1

1

You are using a wrong locator.
Definitely not all the a elements are links, especially the first a on that page.
Try using this instead:

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
import time

wd = webdriver.Firefox()

wd.get("https://www.google.com/search?q=python")

# wd.find_element_by_css_selector("a").click()  # This doesn't work, same error
WebDriverWait(wd, 20).until(
    expected_conditions.element_to_be_clickable((By.XPATH, "//div[@id='search']//a[contains(@href,'http')]"))
).click()

time.sleep(5)

wd.close()

Also, no need to define such a long timeouts for the explicit wait. 20-30 seconds are enough in most cases.

Sign up to request clarification or add additional context in comments.

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.