gpt4 book ai didi

python - Selenium Python - 切换到框架后在框架外找到标题/标题

转载 作者:行者123 更新时间:2023-11-28 18:09:42 25 4
gpt4 key购买 nike

我试图通过断言头部中的 title 等于 'Sales and Offering Space' 来检查是否打开了正确的网页。此 head/title 在 iframe 元素中并且不可见。

在我切换到 iframe 之后,Selenium 在 iframe 中找不到想要的 title('Sales and Offering Space')。相反,它在框架('E-banking')之外找到主页 title,我的断言失败了。

我尝试通过 xpath(abs、rel、索引)和标签名称定位正确的 title,但它们总是只能找到主标题。有人可以帮忙吗?

导航到测试页面后,我有以下代码:

context.driver.switch_to_frame('bzeMainIframe')
title = WebDriverWait(context.driver, 5).until(expected_conditions.presence_of_element_located((By.TAG_NAME, 'title')))
assert title.text == 'Sales and Offering Space', "Page title is '%s' instead of 'Sales and Offering Space'" % (context.driver.title)

结果:

Assertion Failed: Page title is 'E-Banking' instead of 'Sales and Offering Space'

在下一步中,我有一个类似的检查,切换到 iframe 并检查正文中元素的值,这个断言是成功的。因此,我认为上述代码的问题可能与所需元素位于头部这一事实有关。

正在测试的应用程序:

https://ebanking-demo-ch2.ubs.com/auth/login1- 点击登录,然后等待几秒钟被重定向,然后点击顶部菜单上的产品。

该项目使用 Behave,还有很多代码我无法在此处复制,但如果需要,我会将所有内容推送到 github。

最佳答案

如果 title 被隐藏,那么 title.text 将返回空字符串。此外,context.driver.title 总是返回主页的标题。所以

  • title.text == "",
  • context.driver.title == "电子银行",
  • title.get_attribute('textContent') == "Sales and Offering Space"

您可以尝试修改您的代码如下:

context.driver.switch_to_frame('bzeMainIframe')
title = WebDriverWait(context.driver, 5).until(expected_conditions.presence_of_element_located((By.TAG_NAME, 'title')))
assert title.get_attribute('textContent') == 'Sales and Offering Space', "Page title is '%s' instead of 'Sales and Offering Space'" % (title.get_attribute('textContent'))

关于python - Selenium Python - 切换到框架后在框架外找到标题/标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51464467/

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