1

I am trying to find and click a close button on an iframe.

<modal-container class="modal fade show" role="dialog" tabindex="-1" style="display: block;" aria-modal="true"><div role="document" class="modal-dialog modal-xl"><div class="modal-content"><div _ngcontent-gtk-c9="" class="modal-header card-header modal-header-news-expanded"><h4 _ngcontent-gtk-c9="" align="center" class="modal-title col-11 text-center">Article Title PDF </h4><button _ngcontent-gtk-c9="" aria-label="Close" class="close close_white_color" type="button"><span _ngcontent-gtk-c9="" aria-hidden="true">×</span></button></div><br _ngcontent-gtk-c9=""><div _ngcontent-gtk-c9="" class="container-fluid"><!----><div _ngcontent-gtk-c9="" class="row"><!----><div _ngcontent-gtk-c9="" class="col-md-12 ng-star-inserted"><!----><iframe _ngcontent-gtk-c9="" id="pdf_iframe_outside_modal" src="link.com" class="ng-star-inserted" cd_frame_id_="337af673cf64fd1888fcd2afe645984c"></iframe><!----></div></div><!----></div></div></div></modal-container>

I have tried the following methods:

Try1:

close = driver.find_element_by_xpath("//button[@aria-label=Close']").click()

Try2:

close = driver.find_element_by_xpath("//button[@class='close close_white_color']").click()

Try3:

close = driver.find_element_by_xpath("//button[contains(@class,\"close close_white_color\")]").click()

Which gives following error

Error1:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@aria-label='Close']"}

Error2:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[@class='close close_white_color']"}

Error3:

NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//button[contains(@class,"close close_white_color")]"}

I am able to interact with the iframe but unable to locate this button. Any suggestions would be appreciated

2
  • Can you share a link to that page or at least all that page HTML and more of your code? Commented Dec 24, 2021 at 12:45
  • @Prophet Have added extended page HTML Commented Dec 24, 2021 at 13:14

1 Answer 1

1

A couple of things here:

  • The element isn't within the iframe
  • click() doesn't returns anything.

Solution

The desired element is within a Modal Dialog Box, to click on the clickable element you need to induce WebDriverWait for the element_to_be_clickable() and you can use either of the following Locator Strategies:

  • Using CSS_SELECTOR:

    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button.close.close_white_color[aria-label='Close'] > span"))).click()
    
  • Using XPATH:

    //button[@class='close close_white_color' and @aria-label='Close']/span
    
  • 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
    
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @DebanjanB. You are correct, I have an iframe within a Modal Dialog Box. I want to download the pdf which is within the iframe and then click 'close' (as in the question) located in the Modal Dialog Box. Once i switch to iframe and download the pdf, i am unable to run the your solution, it gives me an error StaleElementReferenceException: stale element reference: element is not attached to the page document.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.