场景:1.打开Amazon.in 2.搜索一个项目=“恶意” 3.添加到购物车。 错误消息:NoSuchElementException:消息:无法找到元素。 代码如下:-
class ViewProductDetails(unittest.TestCase):
page_url = "https://www.amazon.in"
search_item = "Malice"
@classmethod
def setUp(cls) -> None:
cls.driver = webdriver.Firefox()
cls.driver.maximize_window()
cls.driver.delete_all_cookies()
cls.driver.set_page_load_timeout(15)
def test_AMZN_TC003_add_item_to_cart(self):
self.driver.get(self.page_url)
searchTB = self.driver.find_element_by_id("twotabsearchtextbox")
searchTB.clear()
searchTB.send_keys(self.search_item)
searchTB.send_keys(Keys.RETURN)
Item = self.driver.find_element_by_xpath("//span[@class='celwidget slot=SEARCH_RESULTS template=SEARCH_RESULTS widgetId=search-results index=0']//span[@class='a-size-medium a-color-base a-text-normal'][contains(text(),'Malice')]")
wait = WebDriverWait(self.driver, 25)
element = wait.until(ec.element_to_be_clickable(Item))
element.click()
self.driver.switch_to.window(self.driver.window_handles[1])
self.driver.find_element_by_id("add-to-cart-button").click()
self.assertTrue(self.driver.find_element_by_id("hlb-subcart").is_enabled())
self.assertTrue(self.driver.find_element_by_xpath("//h1[@class='a-size-medium a-text-bold']").is_displayed())
print("Item added to the cart successfully!!")
我已经尝试过使用隐式等待和time.sleep(),但结果相同。