Select iframe using Python + Selenium

后端 未结 6 751
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:46

    This worked for me with Python (v. 2.7), webdriver & Selenium when testing with iframes and trying to insert data within an iframe:

    self.driver = webdriver.Firefox()
    
    ## Give time for iframe to load ##
    time.sleep(3)
    ## You have to switch to the iframe like so: ##
    driver.switch_to.frame(driver.find_element_by_tag_name("iframe"))
    ## Insert text via xpath ##
    elem = driver.find_element_by_xpath("/html/body/p")
    elem.send_keys("Lorem Ipsum")
    ## Switch back to the "default content" (that is, out of the iframes) ##
    driver.switch_to.default_content()
    

提交回复
热议问题