当我运行' def'它们分别起作用,它们给出了正确的结果。然而,当我结合' def' Promos的功能在一个,它给出了一个错误。 无法解决问题所在。 Selenium新手。请帮忙。
import unittest
from selenium import webdriver
class elementbyid (unittest.TestCase):
@classmethod
def setUpClass(cls):
cls.driver = webdriver.Chrome()
cls.driver.implicitly_wait(10)
cls.driver.maximize_window()
cls.driver.get("http://demo.magentocommerce.com/")
def test_vip_promo(self):
vippromo = self.driver.\
find_element_by_xpath("//img[@alt='Shop Private Sales - Members Only']")
self.assertTrue(vippromo.is_displayed())
vippromo.click()
self.assertEqual("VIP", self.driver.title)
self.driver.get("http://demo.magentocommerce.com/")
def test_home_decor(self):
hdecor = self.driver.\
find_element_by_xpath("//img[@alt='Physical & Virtual Gift Cards']")
self.assertTrue(hdecor.is_displayed())
hdecor.click()
self.assertEqual("Home & Decor", self.driver.title)
self.driver.get("http://demo.magentocommerce.com/")
def test_bags_luggage(self):
blug = self.driver.\
find_element_by_xpath("//img[@alt='Travel Gear for Every Occasion']")
self.assertTrue(blug.is_displayed())
blug.click()
self.assertEqual('Bags & Luggage - Accessories', self.driver.title)
self.driver.get("http://demo.magentocommerce.com/")
@classmethod
def tearDownClass(cls):
cls.driver.quit()
答案 0 :(得分:0)
我认为当你背靠背运行这些功能时,页面没有时间加载。
Selenium IDE有一个命令“waitForVisible”,它在Python WebDriver导出中实现,如下所示:
for i in range(60):
try:
if driver.find_element_by_id("first_name").is_displayed(): break
except: pass
time.sleep(1)
else: self.fail("time out")
如果将其设置为等待所需页面的唯一元素,那么您的断言将针对正确的页面运行。
How to get Selenium to wait for page load after a click上的这篇博客文章也值得一读。