3

I'm trying to login into a page (https://www.aprovaconcursos.com.br/questoes-de-concurso/cliente/login) using selenium, chromedriver and python. But although the element is easy to find, I can't figure out a way to interact with it.

I'm running it with python 3.7, chromedriver and windows 10. Don't think this might be the problem. It seems that there is some element covering the input I'm trying to inser data into, but can't figure what.

This is the code I'm using to do it.

**def login(username, senha):

    #entra no site e faz login

    driver.get('https://www.aprovaconcursos.com.br/questoes-de-concurso/cliente/login')
    time.sleep(30)
    site_username = driver.find_element_by_id("username")
    site_username.clear()
    site_username.send_keys(username)
    site_password = driver.find_element_by_id("password")
    site_password.clear()
    site_password.send_keys(senha)
    driver.find_element_by_class_name("btn btn-primary").click()**

The problem is the following:

Traceback (most recent call last):
   File "***.py", line 161, in <module>
        login(username,senha)
   File "***.py", line 125, in login
        site_username.clear()
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 95, in clear
        self._execute(Command.CLEAR_ELEMENT)
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webelement.py", line 633, in _execute
        return self._parent.execute(command, params)
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\webdriver.py", line 321, in execute
        self.error_handler.check_response(response)
   File "C:\Users\***\AppData\Local\Programs\Python\Python37-32\lib\site-packages\selenium\webdriver\remote\errorhandler.py", line 242, in check_response
        raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.ElementNotVisibleException: Message: element not interactable   (Session info: chrome=73.0.3683.103)   (Driver info: chromedriver=73.0.3683.68 (47787ec04b6e38e22703e856e101e840b65afe72),platform=Windows NT
    10.0.17134 x86_64)

EDIT

The problem does not seem to be related with a EC. Tried the solutions provided but the condition never arises.

2 Answers2

0

Instead of using sleep commands, it is much more advisable to use wait conditions. You can wait for a certain condition to present itself before moving on. This should help avoid issues with elements not being visible, clickable, etc.

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
element = wait.until(EC.visibility_of_element_located((By.ID, 'username')))

You can find more help on waits at these links:

https://selenium-python.readthedocs.io/waits.html

https://seleniumhq.github.io/selenium/docs/api/py/webdriver_support/selenium.webdriver.support.expected_conditions.html

RKelley
  • 1,099
  • 8
  • 14
  • Thank you for your help. Tried it and the code worked as expected: the webdriver waited. But the problem is that it waits until forever. I think there is something above the input boxes that I'm not seeing and this is the cause of the problem. – great_coder Apr 23 '19 at 13:11
0

You should add while loop, for example:

`

int x = 0;
while (x < 3) {
try {
WebElement we = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("xpath")));
we.click();
break;
} catch (WebDriverException e) {
}
x++;
}

` adding these few extra lines of code will make clicking more robust and script will try clicking on element in loop.

eddieB
  • 43
  • 1
  • 6
  • Thank you. The problem holds on the EC: it never comes. I think I'm missing something in the way those input boxes are built. – great_coder Apr 23 '19 at 13:13