我正在试着切换到这个框架

然而,我一直收到这样的错误:
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')发布于 2018-10-31 15:50:24
根据超文本标记语言,要切换到所需的框架,您需要使用WebDriverwait使框架可用并切换到它,您可以使用以下解决方案之一:
CSS_SELECTOR:驱动程序( 10).until(EC.frame_to_be_available_and_switch_to_it((By.CSS_SELECTOR,"iframeid^='layui-layer-iframe'")))
XPATH:,WebDriverWaitWebDriverWait(driver,10).until(EC.frame_to_be_available_and_switch_to_it((By.XPATH,"//iframestarts-with(@id,'layui-layer-iframe') )和开头-(@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上找到相关的讨论
发布于 2018-10-31 15:21:10
你做错了,一定是layui-layer-iframe6,试试这个,
browser.switch_to.frame(browser.find_element_by_name("layui-layer-iframe6"))并再次返回到主窗口使用,
browser.switch_to.default_content()由于回溯会产生等待,所以wait也可以使用WebDriverWait()。
发布于 2018-10-31 15:55:17
问题解决了。我使用了以下代码:
browser.switch_to.frame(browser.find_element_by_xpath('//*[@id="layui-layer-iframe6"]'))https://stackoverflow.com/questions/53077744
复制相似问题