简体   繁体   中英

How to click on the button when the textContext contains leading and trailing white-space characters?

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:

在此处输入图片说明

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM