gpt4 book ai didi

python - 切换到帧 : NoSuchFrameException: Message: no such frame 时出现 Selenium 错误

转载 作者:行者123 更新时间:2023-12-05 05:13:56 29 4
gpt4 key购买 nike

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

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

最佳答案

根据要切换到所需框架的 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 的相关讨论

关于python - 切换到帧 : NoSuchFrameException: Message: no such frame 时出现 Selenium 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53077744/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com