Problem using python Selenium click()

Asked

Viewed 1,877 times

0

Hello, I’m starting to use Selenium. I started trying to access google and some basic commands. However I can’t use click(). My show:

from selenium import webdriver

class Google(object):
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://google.com'
        self.search_bar = 'q'
        self.btn_search = 'btnK'
        self.btn_lucky = 'btnI'

    def navigate(self):
        self.driver.get(self.url)

    def search(self):
        self.pesquisa = 'sabonete'
        self.driver.find_element_by_name(self.search_bar).send_keys(self.pesquisa)
        self.driver.find_element_by_name('btnI').click()


gg = webdriver.Chrome()

g = Google(gg)

g.navigate()
g.search()

Error:

selenium.common.exceptions.ElementNotInteractableException: Message: element not interactable
  (Session info: chrome=81.0.4044.138)

3 answers

0

According to A edureka! Communinity - How do I resolve the Elementnotinteractableexception in Selenium Webdriver, ElementNotInteractableException occurs when the element is found, but cannot interact with the element, and cannot send a click. This can happen due to several reasons:

  1. element may not be visible on the screen or is not shown on the screen
  2. element is off the screen
  3. element is behind another element
  4. html element is hidden (Hidden="true")
  5. the javascript of the page is still "mounting" the page and the button has not yet appeared on the screen.

I played the problem here, the solution was to put a wait time before trying to click the button.

Example:

import time
from selenium import webdriver
driver = webdriver.Firefox()
driver.get('https://www.google.com')
search_bar = driver.find_element_by_name('q')
search_bar.send_keys('texto da pesquisa')
btn_lucky  = driver.find_element_by_name('btnI')
time.sleep(1)  # Aguarda 1 segundo antes de clicar, foi o suficiente para resolver o erro.
btn_lucky.click()

This is probably caused by the way Google builds the elements on the page dynamically with javascript, when Selenium tries to click the button not yet rendered on the screen.

I hope I’ve helped.

0

I performed a test and got the same error "element not interactable".

I have verified that this occurs because you have not closed your driver with the driver quit.(), then Chrome continues to load in memory when your program leaves, and the next time you try to run the program it launches another instance of Chrome, probably the driver gets lost and connects to the wrong instance of Chrome and starts giving this error. You should always close the driver at the end of your program to avoid problems.

To resolve the error:

  1. First kill all instances of memory Chrome with the command $ pkill chrome
  2. Put the remote driver.quit() at the end of your program.
  3. Run your program again, it should work properly without error.
  • 1

    Yes. That was on purpose, I did not execute the driver.quit(), purpose so the guy can see the result on screen, and close manually after checking that the command has been executed successfully. I can edit my reply and make that information explicit.

-1

I have never tried to interact with a page this way, but this error you are getting is saying that it is not possible to interact with the selected element.

An alternative way to achieve the same result is by using the element input from the google home page. For this just pick up the classe css page input element.

In my example I used the xpath to fill in the values of input text and click the button pesquisa do Google

Follow an example:

class Google(object):
    def __init__(self, driver):
        self.driver = driver
        self.url = 'http://google.com'

    def navigate(self):
        self.driver.get(self.url)

    def search(self):
        self.pesquisa = 'sabonete'
        self.driver.find_element_by_xpath('/html/body/div/div[3]/form/div[2]/div[1]/div[1]/div/div[2]/input').send_keys(self.pesquisa)
        self.driver.find_element_by_xpath('//*[@id="tsf"]/div[2]/div[1]/div[3]/center/input[1]').click()



gg = webdriver.Chrome()

g = Google(gg)

g.navigate()
g.search()

If you need help, you can talk here.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.