0

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' 
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
user3533755
  • 111
  • 2
  • 12

2 Answers2

0

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
Prophet
  • 32,350
  • 22
  • 54
  • 79
  • `driver.switch_to.frame(iframes[0])` even If ignore the for loop and just try this, I met with the same error. Also, `if 'ontouchmove' in iframe:` is behaving correctly and returns the proper iframe. – user3533755 Jan 10 '22 at 14:58
  • If I do `print(type(iframes[0]))` it returns a `dict`. – user3533755 Jan 10 '22 at 15:00
  • Hm... Can you try my updated solution? – Prophet Jan 10 '22 at 15:02
  • same error as I had, `driver.switch_to.frame(iframe)` produces error `selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT'` – user3533755 Jan 10 '22 at 15:04
  • What gives you `print(len(iframes))`? – Prophet Jan 10 '22 at 15:05
  • 10. I added the most basic test case to my original post, just taking the first iframe found, same error. – user3533755 Jan 10 '22 at 15:06
  • Are there all iframes on the same level or some of them nested inside other iframes? – Prophet Jan 10 '22 at 15:06
  • They are all iframes on the same level. All iframes are within the same div, I am counting exactly 10, looking at the page source code. Which matches with the iframes count. – user3533755 Jan 10 '22 at 15:08
  • In case the first iframe gives the same error it sounds strange. Need to actually debug that... – Prophet Jan 10 '22 at 15:09
  • Is it possible to get a reference to an element within a visible iframe without switching to it to find it? Because in the end, all I have to do is `driver.execute_script("arguments[0].click();", elem)` on the element. And I can locate the element by searching for `aria-label='Press & Hold'` – user3533755 Jan 10 '22 at 15:17
  • AFAIK you can not do it with Selenium. Maybe with some JavaScript, however I'm not familiar with such option. – Prophet Jan 10 '22 at 15:24
0

'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)
    
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352