Python selenium locate element in multiple iframes

后端 未结 2 536
说谎
说谎 2021-01-24 23:57

I am new to Selenium for Python and was trying to locate element in multiple iframes. This is the DOM element I can see.



        
2条回答
  •  温柔的废话
    2021-01-25 00:23

    In general, when navigating frames in Selenium, the following method is probably most reliable.

    On every single frame change, go back to the root frame, or default:

    driver.switch_to.default_content()
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//Some XPATH here')))
    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH, '//Some Xpath here')))
    

    In your case, switch to default, then switch to first child, then next child, and so on. On the next frame switch, repeat this - first to default, then to 1st child, etc.

    I'd also add the you are searching for frames by tag_name, which is not very specific. How many tags with that tag_name are there in the whole document?

    If there really is no unique id or name on that frame, you can search by the frame's src using something like this:

    By.XPath("//iframe[contains(@src,'

提交回复
热议问题