在另一个框架集

时间:2016-06-19 13:14:13

标签: python selenium selenium-webdriver

如何访问位于另一个框架集内的框架集内的框架?

enter image description here

这是我的代码,它返回selenium.common.exceptions.NoSuchFrameException: Message: no such frame

from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = webdriver.Chrome()
driver.get("https://netbanking.hdfcbank.com/netbanking/")
driver.switch_to_default_content()

assert "Welcome to HDFC Bank" in driver.title

driver.switch_to_frame("login_page")

try:
    WebDriverWait(driver, 10).until(EC.presence_of_element_located(driver.find_element_by_class_name('pwd_field')))
    print "Page is ready!"
except TimeoutException:
    print "Loading took too much time!"

driver.implicitly_wait(10)  # seconds
driver.close()

1 个答案:

答案 0 :(得分:2)

您不需要以任何特殊方式处理frameset - 这些只是框架的容器,它们本身不是框架。这对我有用:

  • 添加等待框架出现的等待
  • 切换到相框

代码:

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

driver = webdriver.Chrome()
driver.get("https://netbanking.hdfcbank.com/netbanking/")
wait = WebDriverWait(driver, 10)

assert "Welcome to HDFC Bank" in driver.title

frame = wait.until(EC.presence_of_element_located((By.NAME, 'login_page')))
driver.switch_to.frame(frame)

try:
    wait.until(EC.presence_of_element_located((By.CLASS_NAME, 'pwd_field')))
    print("Page is ready!")
except TimeoutException:
    print("Loading took too much time!")

driver.close()

请注意,我还修复了您已经拥有的等待,替换为:

.until(EC.presence_of_element_located(driver.find_element_by_class_name('pwd_field')))

使用:

.until(EC.presence_of_element_located((By.CLASS_NAME, 'pwd_field')))

我还删除了第一个driver.switch_to_default_content()电话 - 您已经开始在默认内容的范围内操作。