简体   繁体   中英

selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'ELEMENT' error when switching frame in Selenium

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' 

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

'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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

Related Question selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: missing 'cookie' while adding cookies using selenium webdriver selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator error using find_element('username') Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() using Selenium Webdriver through Python Python selenium for write review google maps (selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: invalid locator ) selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error invoking send_keys() using Selenium URL must be a string selenium - selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: 'url' must be a string Selenium By.CSS_SELECTOR selenium.common.exceptions.InvalidArgumentException: Message: invalid argument selenium.common.exceptions.InvalidArgumentException: Message: invalid argument: File not found error while uploading file using Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument error invoking get() with urls read from text file with Selenium Python selenium.common.exceptions.InvalidArgumentException: Message: invalid argument while iterating through a list of urls and passing as argument to get()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM