如何使用 Selenium C# 单击按钮

我需要使用 selenium 来点击按钮,但我遇到了一些问题


我尝试了这段代码,但显示错误“Selenium.InvalidSelectorException: 'invalid selector”


IList link = driver.FindElements(By.ClassName("button postfix"));


        foreach (IWebElement elem in link)

        {

            if (elem.GetAttribute("ng-click").Equals("quickSearch.search()"))

                elem.Click();

        }

html页面代码


<a href="javascript: void(0);" class="button postfix" ng-click="quickSearch.search()" analytics-on="click" analytics-event="InventoryManagementSearchKeyword" sc-omniture-props="InventoryManagementAllSS"><i class="fi-magnifying-glass"></i></a>

我尝试使用 id 但按钮没有 Id,所以我不知道如何使用它


冉冉说
浏览 204回答 2
2回答

拉丁的传说

由于该元素是一个Angular元素,因此要调用click()所需的元素,您必须为ElementToBeClickable引入WebDriverWait并且您可以使用以下任一定位器策略:CssSelector:new&nbsp;WebDriverWait(driver,&nbsp;TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a.button.postfix[ng-click^='quickSearch'][analytics-event='InventoryManagementSearchKeyword']"))).Click();XPath:new&nbsp;WebDriverWait(driver,&nbsp;TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='button&nbsp;postfix'&nbsp;and&nbsp;starts-with(@ng-click,&nbsp;'quickSearch')][@analytics-event='InventoryManagementSearchKeyword']"))).Click();

陪伴而非守候

你可以使用 Xpath。&nbsp;driver.FindElement(By.XPath("//a[@class='button&nbsp;postfix'&nbsp;and&nbsp;@ng-click='quickSearch.search()']")).Click();
打开App,查看更多内容
随时随地看视频慕课网APP