Reputation: 47
I am trying to click a button on webpage using selenium script but it is giving me following error using this line:
driver.find_element_by_class_name('btn-primary').click()
Error is as follows:
ElementNotInteractableException: Message: Element <button class="btn-primary btn-text sort-filter-clear-button" type="button"> could not be scrolled into view
HTML of the button element:
<button type="submit" class="btn-primary btn-action bookButton" id="bookButton" data-track="FLT.RD.Book.Bottom"><span class="btn-label">Continue Booking</span></button>
Upvotes: 3
Views: 3145
Reputation: 5647
Try to wait for element:
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
It will wait at least 10 seconds, until element will be clickable.
Note: you have to add some exports:
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
EDIT: you can try also js executor like this:
button = driver.find_element_by_id("bookButton")
driver.execute_script("arguments[0].click();", button)
in case your button is inside an iframe/frame
, firstly you have to switch to this frame
and only then you can interact with this element:
WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME("frame_name"))))
# do your stuff
button = WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.ID, "bookButton")))
button.click()
driver.switch_to.default_content() # switch back to default content
Upvotes: 3
Reputation: 193338
As per the HTML you have shared and as you mentioned of bootstrap button, moving forward as you are trying to invoke click()
on the desired element you need to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:
CSS_SELECTOR
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn-primary.btn-action.bookButton#bookButton>span.btn-label"))).click()
XPATH
:
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn-primary btn-action bookButton' and @id='bookButton']/span[@class='btn-label'][contains(.,'Continue Booking')]"))).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
Upvotes: 1