1

I'm trying to click on the following button using Selenium with python:

<button type="submit" tabindex="4" id="sbmt" name="_eventId_proceed">
          Einloggen
</button>

This is just a simple button which looks like this:

enter image description here

Code:

driver.find_element_by_id('sbmt').click()

This results in the following exception:

selenium.common.exceptions.ElementNotInteractableException: Message:
Element <button id="sbmt" name="_eventId_proceed" type="submit">
could not be scrolledinto view

So, I tried scrolling to the element using ActionChains(driver).move_to_element(driver.find_elements_by_id('sbmt')[1]).perform() before clicking the button.

(Accessing the second element with [1] because the first would result in selenium.common.exceptions.WebDriverException: Message: TypeError: rect is undefined exception.).

Then I used

wait = WebDriverWait(driver, 5)
submit_btn = wait.until(EC.element_to_be_clickable((By.ID, 'sbmt')))

in order to wait for the button to be clickable. None of this helped.

I also used driver.find_element_by_xpath and others, I tested it with Firefox and Chrome.

How can I click on the button without getting an exception?

Any help would be greatly appreciated

Guy
  • 46,488
  • 10
  • 44
  • 88
jonasstr
  • 57
  • 3
  • 9

1 Answers1

2

To invoke click() on the element you need to first use WebDriverWait with expected_conditions for the element to be clickable and you can use the following solution:

  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@id='sbmt' and normalize-space()='Einloggen']"))).click()
    
  • Note : You have to add the following imports :

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
  • why introduce a complex XPATH when you can just use `By.ID` with the id directly? – Corey Goldberg Jan 05 '19 at 18:57
  • 1
    the OP already stated he tried essentially this same solution.. why did this help? – Corey Goldberg Jan 05 '19 at 18:59
  • @CoreyGoldberg Going by the attributes of the ` – undetected Selenium Jan 05 '19 at 19:19
  • @CoreyGoldberg OP's trials involved `By.ID` but there wasn't any trials including the `innerText` which seems to be the solution to OP's question. – undetected Selenium Jan 05 '19 at 19:25
  • 1
    might be nice to explain that in your answer – Corey Goldberg Jan 05 '19 at 20:50
  • Well, it's obvious the culprit is there are 2 buttons, and the real solution here was just going by the element's text. Thus waiting - to be clickable, to appear, or whatever - is unnecessary, and only adds complexity and execution time. – Todor Minakov Jan 05 '19 at 21:29
  • 1
    @CoreyGoldberg it helped because it showed me how to use the `By.XPATH` correctly. I tried using it but I specified it in the wrong way – jonasstr Jan 06 '19 at 12:38
  • Btw I figured out a much simpler way to solve my problem, which I should have tried before asking the question: The `sbmt` button was part of a name and password input field, so I just used `Keys.ENTER` instead. – jonasstr Jan 06 '19 at 12:42