Python Selenium Webdriver - Navigating nested frame sets

前端 未结 2 744
忘掉有多难
忘掉有多难 2020-12-11 07:25

I\'m working on a page with nested frames, and am unable to access a child frame. Brief outline of HTML source:


    
           


        
相关标签:
2条回答
  • 2020-12-11 08:08

    Try this:

    driver.switch_to_frame("name")
    driver.switch_to_frame("mid2")
    

    The issue you're running into is that javascript can only work with the current frame it sees. mid2 is in a child frame it cannot see if you're at the top of the document.

    0 讨论(0)
  • 2020-12-11 08:19

    The <frame> tag with name as mid2 seems to be nested within 2 additional layers of <frame>.

    to access the <frame> with name attribute as mid2 you have to:

    • Ignore (safely) the presence of parent / child <frameset> tags.
    • Induce WebDriverWait for the first layer of <frame> to be available and switch to it.
    • Induce WebDriverWait for the second layer of <frame> to be available and switch to it.
    • Induce WebDriverWait for the desired <frame> to be available and switch to it.
    • You can use the following solution:

      • Code Block:

        WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.NAME,"name")))
        WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"frame[name='mid1']")))
        WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//frame[@name='mid2' and starts-with(@src, 'chkclineversion')]")))
        
      • 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
        

    You can find a relevant discussion in How to locate and click on an element which is nested within multiple frame and frameset through Selenium using Webdriver and C#


    Outro

    • How can I select a html element no matter what frame it is in in selenium?
    • Ways to deal with #document under iframe
    0 讨论(0)
提交回复
热议问题