Selenium Webdriver提供NoSuchFrameException

时间:2015-02-28 04:37:23

标签: python python-2.7 selenium selenium-webdriver

我想在网页中选择框架,但无法在Selenium IDE中选择框架。当我使用Webdriver时,会出现NoSuchFrameException

driver.find_element_by_name("GO").click()
driver.find_element_by_id("EO_PE_SRCH_INP_EO_PE_URLTEXT$5").click()
driver.switch_to.frame("contentFrame")
  

driver.switch_to.frame(“contentFrame”)文件   “/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/switch_to.py”   第64行,在框架中       self._driver.execute(Command.SWITCH_TO_FRAME,{'id':frame_reference})文件   “/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/webdriver.py”   第173行,执行中       self.error_handler.check_response(response)文件“/Library/Python/2.7/site-packages/selenium-2.44.0-py2.7.egg/selenium/webdriver/remote/errorhandler.py”,   第166行,在check_response中       raise exception_class(message,screen,stacktrace)NoSuchFrameException:消息:无法找到frame:contentFrame   堆栈跟踪:       在FirefoxDriver.prototype.switchToFrame(file:///var/folders/nk/l6lw3w5917l99dl9vs0g6z_m0000gn/T/tmp4NI1ne/extensions/fxdriver@googlecode.com/components/driver-component.js:9710:11)       在DelayedCommand.prototype.executeInternal_ / h(file:///var/folders/nk/l6lw3w5917l99dl9vs0g6z_m0000gn/T/tmp4NI1ne/extensions/fxdriver@googlecode.com/components/command-processor.js:11635:16)       at fxdriver.Timer.prototype.setTimeout /< .notify(file:///var/folders/nk/l6lw3w5917l99dl9vs0g6z_m0000gn/T/tmp4NI1ne/extensions/fxdriver@googlecode.com/components/command-processor.js:548:5 )

HTML code:

<head></head>
<frameset id="fm2" frameborder="0" cols="150,*" framespacing="0" border="0">
    <frame src="https://xxxxxxxxxxx/servlet/Main/menu" scrolling="yes" name="mainMenuFrame"></frame>
    <frame noresize="" name="contentFrame" src="https://xxxxx/servlet/Main/main?usertype=2">
        #document
    </frame>
</frameset>

1 个答案:

答案 0 :(得分:0)

这是你要处理的frameset,分两步完成:

frame = driver.find_element_by_id("fm2")
driver.switch_to.frame(frame)

driver.switch_to.frame("contentFrame")

您还可以wait for the frame explicitly

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

frame = WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.ID, "fm2")))
driver.switch_to.frame(frame)

driver.switch_to.frame("contentFrame")

更新(使用评论中提供的真实示例):

您似乎需要通过索引切换到框架

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


options = webdriver.ChromeOptions()
options.add_argument('--ignore-certificate-errors')

driver = webdriver.Chrome(chrome_options=options)
driver.get('https://www.ihp.hku.hk/sfb.html')

WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.TAG_NAME, "frameset")))
driver.switch_to.frame(1)

driver.find_element_by_link_text('Badminton: max 4 persons per court').click()