Search code examples
pythonseleniumwebdrivernosuchelementexception

Selecting item in nested html Frame with Selenium WebDriver


I'm attempting to use Selenium WebDriver to select a link named cslLogin on a web page. It's located in a child frame called topframe but I can't access it, even after I switch to its parent frame TopLevelFrame which I can do successfully. The html page has this basic layout:

<html>
 <head>...</head>
  <frameset name="ATopLevelFrameSet">
   <frame name="TopLevelFrame">
    #document
     <html>
      <head></head>
       <frameset name="Aframeset">
        <frame name="topframe">
         #document
          <html>
           <head>...</head>
           <body class="clsBgColor">
            <table id="tblTitle">
             <tbody>
              <tr class="clsBackYellow"">
               <td class="clsDeviceStatusLink">
                <a class="clsLogin" href="javascript:void(0);"
                onclick="javascript:fnnLoginClick();">Login</a> == $0
               etc...

I can successfully switch to TopLevelFrame using self.driver.switch_to.frame("TopLevelFrame") but I cannot then access topframe or clsLogin (I get NoSuchFrameException and NoSuchElementException, respectively)

I've tried find_element_by_name, find_element_by_xpath, find_element_by_link_text, find_element_by_css_selector, and have also used

try:
    element = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located((By.NAME, "TopLevelFrame"))
            )
finally:
    self.driver.quit()

in case it was a time/page loading issue, but it times out long after the page loads.

I know from other posts that I need to switch to the nearest frame first before I can access the element, but of course this isn't working. Any suggestions? Thanks in advance.


Solution

  • To click the link cslLogin first you have to switch to the TopLevelFrame <frame> then to topframe <frame> and then click on the link as follows :

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"TopLevelFrame"))
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(By.NAME,"topframe"))
    WebDriverWait(driver, 10).until(EC.element_to_be_clickable((By.LINK_TEXT, "Login"))).click()
    # Or
    WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//tr[@class='clsBackYellow']/td[@class='clsDeviceStatusLink']/a[@class='clsLogin']"))).click()