为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 Selenium C# 单击按钮

如何使用 Selenium C# 单击按钮

C#
冉冉说 2022-12-24 12:25:10
我需要使用 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,所以我不知道如何使用它
查看完整描述

2 回答

?
拉丁的传说

TA贡献1789条经验 获得超8个赞

由于该元素是一个Angular元素,因此要调用click()所需的元素,您必须为ElementToBeClickable引入WebDriverWait并且您可以使用以下任一定位器策略


  • CssSelector:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("a.button.postfix[ng-click^='quickSearch'][analytics-event='InventoryManagementSearchKeyword']"))).Click();
  • XPath:

    new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//a[@class='button postfix' and starts-with(@ng-click, 'quickSearch')][@analytics-event='InventoryManagementSearchKeyword']"))).Click();



查看完整回答
反对 回复 2022-12-24
?
陪伴而非守候

TA贡献1757条经验 获得超8个赞

你可以使用 Xpath。

 driver.FindElement(By.XPath("//a[@class='button postfix' and @ng-click='quickSearch.search()']")).Click();



查看完整回答
反对 回复 2022-12-24
  • 2 回答
  • 0 关注
  • 203 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号