Home > OS >  Selenium how to click modal close button
Selenium how to click modal close button

Time:11-22

I tried many combinations but couldn't manage to click on the X button to close the modal window.

According to the html code below

<div class="modal-close" data-dismiss="modal">
                    <i class="far fa-times"></i>
                </div>

What string should i pass below for finding the element by css selector?

driver.find_element_by_css_selector()

CodePudding user response:

According to HTML you are sharing in the question the CSS Selector you should use can be

driver.find_element_by_css_selector(div.modal-close)

However I can't be sure about it since I can't check if this locator is unique etc.
Also you possibly should use some wait / delay before using this command.

CodePudding user response:

I moved from css_selector to class_name

driver.find_element_by_class_name("modal-close").click()

This resolved my issue.

CodePudding user response:

If you want to perform a click action with selenium, you should store the element to click, close = drive.find_element_by_css_selector('.modal-close').

After you store the element, you should be able to click it using .click():
close.click()

If you don't want to store it, simply call: drive.find_element_by_css_selector('.modal-close').click().

  • Related