3

I'm trying to switch to this frame

enter image description here

However, I kept getting this error:

Traceback (most recent call last):
  File "/Users/yaoweiqi/PycharmProjects/learnselenium/test1.py", line 29, in <module>
    browser.switch_to.frame('layui-layer-iframe6')
  File "/Users/yaoweiqi/PycharmProjects/learnselenium/venv/lib/python3.6/site-packages/selenium/webdriver/remote/switch_to.py", line 89, in frame
    self._driver.execute(Command.SWITCH_TO_FRAME, {'id': frame_reference})
  File "/Users/yaoweiqi/PycharmProjects/learnselenium/venv/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
    self.error_handler.check_response(response)
  File "/Users/yaoweiqi/PycharmProjects/learnselenium/venv/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
    raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.NoSuchFrameException: Message: no such frame
  (Session info: chrome=70.0.3538.77)
  (Driver info: chromedriver=2.43.600229 (3fae4d0cda5334b4f533bede5a4787f7b832d052),platform=Mac OS X 10.13.1 x86_64)

Can anyone tell me what am I doing wrong? Thanks a lot. Following is my code:

from selenium import webdriver
import time
from wait_element import wait_element

browser = webdriver.Chrome('/Users/yaoweiqi/Downloads/chromedriver')
browser.get('http://47.99.113.178/index.html')

wait_element(driver=browser, xpath='//input[@class="user-aqm-input"]', action='input', keys='***')
wait_element(driver=browser, xpath='//input[@class="user-user-input"]', action='input', keys='***')
wait_element(driver=browser, xpath='//input[@class="user-pwd-input"]', action='input', keys='***')
wait_element(driver=browser, xpath='//button[@class="user-submit determine"]', action='click')
try:
    wait_element(driver=browser, xpath='//img[@class="updataclose"]', action='click')
except:
    print('notice already closed')

wait_element(driver=browser, xpath='//*[@class="icon-jiahao"]', action='click')
wait_element(driver=browser, xpath='//*[@class="housing-type"]', action='click')

time.sleep(5)

frames = browser.find_element_by_tag_name('iframe')
browser.switch_to.frame('layui-layer-iframe6')
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
Jade
  • 100
  • 1
  • 1
  • 8

4 Answers4

2

As per the HTML to switch to the desired frame you need to use WebDriverwait for the frame to be available and switch to it and you can use either of the following solutions:

  • CSS_SELECTOR:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframe[id^='layui-layer-iframe'][src^='fangyuan']")))
    
  • XPATH:

    WebDriverWait(driver, 10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframe[starts-with(@id,'layui-layer-iframe') and starts-with(@src,'fangyuan')]")))
    

Note : You have to add the following imports :

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

Here you can find a relevant discussion on Ways to deal with #document under iframe

undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
1

you are doing wrong, ayui-layer-iframe6 it must be layui-layer-iframe6 try this,

browser.switch_to.frame(browser.find_element_by_name("layui-layer-iframe6"))

and again come back to main window use,

browser.switch_to.default_content()

as traceback gives NoSuchFrameException so putting wait would also work WebDriverWait().

Ashish Kamble
  • 2,555
  • 3
  • 21
  • 29
  • Thanks, but I've changed my code but I'm still getting this error. – Jade Oct 31 '18 at 07:28
  • Read carefully my answer i have told you to write `browser.find_element_by_name("layui-layer-iframe1")` not just `layui-layer-iframe1` – Ashish Kamble Oct 31 '18 at 07:29
  • I tried your code. Now I'm getting this error: selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"name","selector":"layui-layer-iframe6"} – Jade Oct 31 '18 at 07:32
  • 2
    `switch_to.frame` receives `id` and `name` as parameter. It isn't necessary to use `find_element_by_` – Guy Oct 31 '18 at 07:33
0

Just a reminder, the name of the iframe might change with the number of clicks.

Here is an example:

for num in range(1, 2):
    frame_id = "layui-layer-iframe" + str(num)
    browser.switch_to.frame(frame_id)
canpop
  • 1
  • 1
-1

Problem solved. I used this code:

browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="layui-layer-iframe6"]'))
Jade
  • 100
  • 1
  • 1
  • 8
  • 1
    Actually `browser.switch_to.frame('layui-layer-iframe6')` means totally the same as `browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="layui-layer-iframe6"]'))`. I can hardly believe that this line could solve your issue – Andersson Oct 31 '18 at 08:27