切换到框架时出现硒错误:NoSuchFrameException:消息:没有这样的框架

时间:2018-10-31 06:45:45

标签: python selenium xpath css-selectors webdriverwait

我正在尝试切换到此框架

enter image description here

但是,我一直收到此错误:

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)

有人可以告诉我我在做什么错吗?非常感谢。 以下是我的代码:

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

3 个答案:

答案 0 :(得分:1)

根据要切换到所需框架的HTML,您需要使用 WebDriverwait 使框架可用并切换到您可以使用以下任一解决方案:

  • 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')]")))
    

注意:您必须添加以下导入:

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

在这里您可以找到有关Ways to deal with #document under iframe的相关讨论

答案 1 :(得分:0)

您做错了,ayui-layer-iframe6必须为layui-layer-iframe6 试试这个,

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

再次回到主窗口使用,

browser.switch_to.default_content()

由于回溯提供了NoSuchFrameException,因此等待WebDriverWait()也可以工作。

答案 2 :(得分:-1)

问题解决了。我使用了这段代码:

browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="layui-layer-iframe6"]'))