0

I'm using Python 3.9, chromedriver 101 and the latest version of Selenium I'm trying to select and click an element within a frame without an ID. The HTML for the frame and element I want to click on are

<iframe title="false" class="k-content-frame" src="https://publisher.content2classroom.com/viewer.html#viewPublisherInstitutions?winId=ManagePublisherInstitutions" allowfullscreen="" frameborder="0">This page requires frames in order to show content</iframe>
...
    <span id="subpageBackTitle" class="subpage-back-title">&lt; back</span>

I have the following Python, which fails to select the element when it's visible

self.driver.switch_to.default_content()
    ...
element = WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".subpage-back-title")))

The resulting error is

/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/selenium/webdriver/support/wait.py:89: TimeoutException

What's the proper way to navigate to the frame and select/click on the element in question?

Dave
  • 15,639
  • 133
  • 442
  • 830

1 Answers1

0

To click on element inside a frame you need to switch to the frame first.

You can use any selector to switch to a frame. For the iframe .k-content-frame selector should work.

# Store iframe web element
iframe = driver.find_element(By.CSS_SELECTOR, ".k-content-frame")

# switch to selected iframe
driver.switch_to.frame(iframe)

# Your element
element = WebDriverWait(self.driver, 5).until(EC.element_to_be_clickable((By.CSS_SELECTOR, ".subpage-back-title")))

You can use frame_to_be_available_and_switch_to_it expected condition to wait for a frame. All available expected conditions can found here.

WebDriverWait(self.driver, 5).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR, ".k-content-frame")))
Sers
  • 12,047
  • 2
  • 12
  • 31