I am trying to switch to an iframe in selenium (Python). Though, no matter what I try, I keep getting an error.

iframes = driver.find_elements_by_tag_name('iframe')
print(len(iframes))
    for iframe in iframes:
        if 'ontouchmove' in iframe:
            print(iframe)
            driver.switch_to.frame(iframe)

This produces the error -

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT'

I have also tried this -

iframes = driver.find_elements_by_tag_name('iframe')
print(len(iframes))
counter = 0
for iframe in iframes:
    if 'ontouchmove' in iframe:
        print(iframe)
        driver.switch_to.frame(counter)
    counter += 1

this produces the error -

selenium.common.exceptions.JavascriptException: Message: javascript error: frame.setAttribute is not a function

I am not really sure what else I can do here, kind of lost.. any help would be appreciated.

Edit:

iframes = driver.find_elements_by_tag_name('iframe')
print(len(iframes))
driver.switch_to.frame(iframes[0])

This produces error:

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' 
2

There are 2 best solutions below

11
Prophet On

This if 'ontouchmove' in iframe will not work since iframe here is a web element, it is not a string but a web element object. So checking for text string in a web element is an invalid action.
This driver.switch_to.frame(counter) will not work as well since driver.switch_to.frame() intended to accept a web element as a parameter, not an int primitive number.
I'm not sure about the correct code that will work for you since I can't see the web page you are working on, I can only guess the following:
You can switch to those iframes and once you are inside them you can validate presence of element containing the desired text there. Something like this:

iframes = driver.find_elements_by_tag_name('iframe')
print(len(iframes))
for iframe in iframes:
    driver.switch_to.frame(iframe)
    text_elements = driver.find_elements_by_xpath('//*[contains(text(),"ontouchmove")]')
    if text_elements: #list is non-empty, element found
            print("Element found!!!")
    else:
        driver.switch_to.default_content() #element not found, switching back to default content
2
undetected Selenium On

'ontouchmove' is a string where as iframe is a WebElement. So instead of the WebElement you should be looking for the string within the value of the attributes of the WebElement.

  • Looking for the string ontouchmove within the WebElement id:

    iframes = driver.find_elements_by_tag_name('iframe')
    print(len(iframes))
        for iframe in iframes:
            if 'ontouchmove' in iframe.get_attribute("id"):
                print(iframe)
                driver.switch_to.frame(iframe)
    
  • Looking for the string ontouchmove within the WebElement class:

    iframes = driver.find_elements_by_tag_name('iframe')
    print(len(iframes))
        for iframe in iframes:
            if 'ontouchmove' in iframe.get_attribute("class"):
                print(iframe)
                driver.switch_to.frame(iframe)
    
  • Looking for the string ontouchmove within the WebElement title:

    iframes = driver.find_elements_by_tag_name('iframe')
    print(len(iframes))
        for iframe in iframes:
            if 'ontouchmove' in iframe.get_attribute("title"):
                print(iframe)
                driver.switch_to.frame(iframe)