Odiosuu
Odiosuu

Reputation: 33

How to click the close button within a modal window through Selenium and Python

Cannot click button id="close in modal window.

Trying all xpaths like:

//button[@data-dismiss='modal']
//button[@id='close'], //button[@type='button']
//button[contains(.,'Закрыть')]
//button[contains(@data-dismiss,'modal')]
//button[contains(@id,'close')]

Also trying to combine xpaths, but still not working

Code:

<div id="idCardGroupChangeStatusResult" class="modal fade in" tabindex="-1" role="dialog" aria-hidden="false" style="display: block;">
<div class="modal-dialog st-modal-dialog" style="width: 600px; padding-top: 250px;">
<div class="modal-content">
<div class="modal-header st-pad-normal">
<div class="modal-body">
<div class="modal-footer">
<button id="close" class="btn btn-default btn-sm" type="button" data-dismiss="modal"> Закрыть </button>
</div>
</div>
</div>
</div>

Css not working to

Any ideas?

Upvotes: 1

Views: 11318

Answers (2)

Sers
Sers

Reputation: 12255

If element_to_be_clickable not works, try code below to check if there's more than one close buttons on the page. You can use code below to filter by visible or visible and latest one and click on it.

close_buttons = WebDriverWait(driver, 5).until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, "#idCardGroupChangeStatusResult #close")))
# check how many buttons in on the HTML, you can try "visibility_of_all_elements_located"
print(len(close_buttons))

visible_buttons = [close_button for close_button in close_buttons if close_button.is_displayed()]
visible_buttons_len = len(visible_buttons)
print(visible_buttons_len)

visible_buttons[visible_buttons_len - 1].click()

Does the message disappears?

Update, message window disappear:

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.action_chains import ActionChains

driver = webdriver.Chrome()
wait = WebDriverWait(driver, 5)
actions = ActionChains(driver)

#...

status_message = wait.until(
    EC.visibility_of_element_located((By.CSS_SELECTOR, "#idCardGroupChangeStatusResult")))

actions.move_to_element(status_message).perform()
# here you can get text from message window, check/assert ..
status_message.find_element_by_css_selector("#close").click()

Upvotes: 0

undetected Selenium
undetected Selenium

Reputation: 193088

As the element with text as Закрыть is within a Modal Dialog Box so to locate the desired element you have to induce WebDriverWait for the element to be clickable and you can use either of the following solutions:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.btn.btn-default.btn-sm#close[data-dismiss='modal']"))).click()
    
  • Using XPATH:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//button[@class='btn btn-default btn-sm' and @id='close'][@data-dismiss='modal']"))).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: 3

Related Questions