簡體   English   中英

無法定位元素——OpenQA.Selenium.NoSuchElementException

[英]Unable to locate element -- OpenQA.Selenium.NoSuchElementException

我正在嘗試使用 Gherkin 格式通過 Specflow 自動化測試用例,但我不斷收到錯誤消息:

OpenQA.Selenium.NoSuchElementException:沒有這樣的元素:無法找到元素:{“method”:“xpath”,“selector”:“./ancestor-or-self :: form”}

無法定位的按鈕在下方的 AddCart Page 中有注釋。

我正在測試的網站是: http://automationpractice.com/index.php

我想要實現的是如下所示:

Feature: Adding products in the cart

  Scenario Outline: Adding a product of the HomePage in the cart and continue shopping
    Given That the user is on the HomePage
    When User clicks the Add to cart button
    And User clicks the Continue shopping button
    Then The user will stay on the HomePage

步驟定義的代碼是:

namespace WebsiteTestingSpecflow.Steps
{
    [Binding]
    public sealed class AddingCartContinueStep
    {

        AddCartPage addcart = null;

        [Given(@"That the user is on the HomePage")]
        public void GivenThatTheUserIsOnTheHomePage()
        {
            IWebDriver webDriver = new ChromeDriver();
            webDriver.Navigate().GoToUrl("http://automationpractice.com/index.php");
            addcart = new AddCartPage(webDriver);
        }

        [When(@"User clicks the Add to cart button")]
        public void WhenUserClicksTheAddToCartButton()
        {
            addcart.AddCart();
        }

        [When(@"User clicks the Continue shopping button")]
        public void WhenUserClicksTheContinueShoppingButton()
        {
            addcart.ContinueShopping();
        }

        [Then(@"The user will stay on the HomePage")]
        public void ThenTheUserWillStayOnTheHomePage()
        {
            addcart.Verifyelement();
        }

    }
}

AddCart 頁面的代碼是:

namespace WebsiteTestingSpecflow.Pages
{
    public class AddCartPage
    {

        private readonly WebDriverWait wait;

        public IWebDriver Webdriver { get; }

        public AddCartPage(IWebDriver webDriver)
        {
            Webdriver = webDriver;
            wait = new WebDriverWait(webDriver, TimeSpan.FromSeconds(30));
        }

        public IWebElement BtnAddCart => Webdriver.FindElement(By.CssSelector("#homefeatured > .ajax_block_product:nth-child(1) .button:nth-child(1) > span"));
 // This is the button that I keep getting error. 

        public IWebElement btnContinueCart => Webdriver.FindElement(By.CssSelector(".continue > span"));


         public void AddCart() {
            BtnAddCart.Submit(); 
        
        }

        public void ContinueShopping() {

            wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".continue > span")));
            btnContinueCart.Submit();
        
        }

        public void Verifyelement() => Webdriver.FindElement(By.CssSelector(".sfHover > .sf-with-ul"));
    }
}

該按鈕的 CSS 選擇器如 AddCart 頁面中所述,但它仍然無法找到該元素。

有誰知道我該如何解決這個問題?

先感謝您。

看起來您正在嘗試查找尚未顯示的元素,因為只有當您單擊“添加到購物車”時它才會可見,並且只有這樣才能找到該元素。 嘗試下面的代碼並考慮使用頁面 Object model

        public void ContinueShopping() {

        IWebElement btnContinueCart => Webdriver.FindElement(By.CssSelector(".continue > span"));               
        wait.Until(ExpectedConditions.ElementToBeClickable(By.CssSelector(".continue > span")));
        btnContinueCart.Submit();
        }

       

您需要將操作合並到您的代碼中,因為此按鈕不可見,除非您在其上方使用 hover。 還要在它周圍添加一個wait條件

    WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));

    Actions action = new Actions(driver);
    var btn = wait.Until(ExpectedConditions.ElementIsVisible(By.CssSelector("#homefeatured > .ajax_block_product:nth-child(1) .button:nth-child(1) > span")));
    action.MoveToElement(driver.FindElement(By.CssSelector("#homefeatured > .ajax_block_product:nth-child(1) .button:nth-child(1) > span"))).Build().Perform();
    btn.Click();

將以下內容添加到頂部:

using OpenQA.Selenium.Interactions;
using OpenQA.Selenium.Support.UI;

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM