can I embed asterisk wildcard * in python selenium switch_to.frame()

I’ve encountered with a problem. when I need to switch to iframe, the id & name of the iframe varies all the time, from iframe1~iframe9, I am always getting the ‘NoSuchFrameException’,so how can I embed an asterisk wildcard into the iframe’s name, just like :

driver.switch_to.frame(‘iframe*’)

Answer

If there is only ever one iframe, or they’re always in the same order, you can use the index:

driver.switch_to.frame(0)

If it’s more complicated, rather than directly putting the name in the switch_to.frame call, you can search for the frame first, then pass it as a variable; that will let you search by anything, or even loop through all the iframes and check for the one you want in python code.

iframe = driver.find_element_by_xxx(...)
driver.switch_to.frame(iframe)

Leave a Reply

Your email address will not be published. Required fields are marked *