【发布时间】:2019-01-28 20:12:40
【问题描述】:
我正在学习 Selenium。为什么这个简单的测试失败了?
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
from selenium.webdriver.common.action_chains import ActionChains
import unittest
class ToolTipTest (unittest.TestCase):
def setUp(self):
self.driver = webdriver.Firefox()
self.driver.get("http://jqueryui.com/tooltip/")
self.driver.implicitly_wait(30)
self.driver.maximize_window()
def test_tool_tip(self):
driver = self.driver
frame_elm = driver.find_element_by_class_name("demo-frame")
driver.switch_to.frame(frame_elm)
age_field = driver.find_element_by_id("age")
ActionChains(self.driver).move_to_element(age_field).perform()
tool_tip_elm = WebDriverWait(self.driver, 10).until(
expected_conditions.visibility_of_element_located(
(By.CLASS_NAME, "ui-tooltip-content")))
# verify tooltip message
self.assertEqual(
"We ask for your age only for statistical purposes.",
tool_tip_elm.text)
def tearDown(self):
self.driver.close()
if __name__ == "__main__":
unittest.main(verbosity=2)
有时它会提供
AssertionError: 'We ask for your age only for statistical purposes.' != "ThemeRoller: jQuery UI's theme builder app
lication"
- We ask for your age only for statistical purposes.
+ ThemeRoller: jQuery UI's theme builder application
有时:
AssertionError: 'We ask for your age only for statistical purposes.' != "That's what this widget is"
- We ask for your age only for statistical purposes.
+ That's what this widget is
所以错误并不总是相同的。有时它会过去。看起来它选择了一个随机弹出窗口。 Here's 要测试的页面。我正在使用 python 3.6 selenium 3
编辑:我如何(在 Firefox 开发人员工具中)检查工具提示的 html 代码以查看类、id 等?当我选择代码时,工具提示消失,代码也消失了......
【问题讨论】:
-
如果您正在运行您发布的代码并获得不同的结果,那就太奇怪了。我唯一能想到的可能是页面第一次加载时,它会循环显示不同的工具提示,这就是你得到不同结果的原因。您可以尝试在悬停之前为工具提示添加等待不可见,然后悬停,然后检查工具提示。
标签: python selenium assert python-unittest webdriverwait