Select iframe using Python + Selenium

后端 未结 6 753
Happy的楠姐
Happy的楠姐 2020-11-22 12:13

So, I was absolutely baffled as to how to do this in Selenium, and couldn\'t find the answer anywhere, so I\'m sharing my experience.

I was trying to select an ifram

6条回答
  •  清歌不尽
    2020-11-22 12:49

    If iframe is dynamic node, it's also possible to wait for iframe appearence explicitly and then switch to it using ExpectedConditions:

    from selenium.webdriver.support import expected_conditions as EC
    from selenium.webdriver.support.ui import WebDriverWait as wait
    
    driver = webdriver.Chrome()
    driver.get(URL)
    wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it("iframe_name_or_id"))
    

    If iframe doesn't have @id or @name it can be found as common WebElement using driver.find_element_by_xpath(), driver.find_element_by_tag_name(), etc..:

    wait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it(driver.find_element_by_xpath("//iframe[@class='iframe_class']")))
    

    To switch back from iframe:

    driver.switch_to.default_content()
    

提交回复
热议问题