Select iframe using Python + Selenium

后端 未结 6 756
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 13:02

    you can use this simple code to look for the iframe using xpath

    sample use set_iframe("/html/body/div[2]/table/")

    def set_iframe(xpath):
        print('set_iframe')
        driver.switch_to.default_content()
        seq = driver.find_elements_by_tag_name('iframe')
        for x in range(0, len(seq)):
            driver.switch_to.default_content()
            print ("iframe-"+str(x))
            try:
                driver.switch_to.frame(int(x))
                driver.find_element_by_xpath(xpath)
                return str(x)
            except:
                continue
    

提交回复
热议问题