【问题标题】:C# FindElement(By...) with two criteriaC# FindElement(By...) 有两个条件
【发布时间】:2020-05-16 15:05:04
【问题描述】:

我目前正在尝试自动化结帐流程。现在我被卡住了,网站上显示尺寸的按钮几乎相同,它们只是值不同

这就是为什么我想问我是否可以通过类 + 值等两个标准来区分它们。

代码:

using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace selenium1
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            IWebDriver driver = new ChromeDriver();



            driver.Navigate().GoToUrl("https://www.off---white.com/en-de/shopping/off-white-odsy-1000-sneakers-14760681");

            IWebElement element = driver.FindElement(By.());

            element.Click();


        }
    }
}

提前感谢您的帮助!

【问题讨论】:

    标签: c# html selenium button automation


    【解决方案1】:

    您可以使用 CSS 选择器。

    例如:css = element_name[<attribute_name>='<value>']

    所以你的情况是:
    IWebElement element1 = driver.FindElement(By.CssSelector("input[value='25']") IWebElement element2 = driver.FindElement(By.CssSelector("input[value='27']")

    This article should help

    【讨论】:

      【解决方案2】:

      你是对的。您可以将 value 属性与任何其他属性(例如data-test,构造定位器,在 DOM Tree 中唯一标识元素。

      对于文本为 39 的元素上的click(),您需要为所需的ElementToBeClickable() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

      • 使用CssSelector

        driver.Navigate().GoToUrl("https://www.off---white.com/en-de/shopping/off-white-odsy-1000-sneakers-14760681");
        new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.CssSelector("input[data-test='sizeSelector'][value='23']"))).Click();
        
      • 使用XPath

        driver.Navigate().GoToUrl("https://www.off---white.com/en-de/shopping/off-white-odsy-1000-sneakers-14760681");
        new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//input[@data-test='sizeSelector' and @value='23']"))).Click();
        

      【讨论】:

      • 效果很好,但按钮不可交互。我该如何选择尺寸?你能帮我解决这个问题吗?
      • @WalterSommer 查看更新的答案并告诉我状态。
      • ExpectedConditions 带有红色下划线,我无法导入任何内容以将其移除
      • 好吧想通了,如何获得 ExectedConditions,现在所有尺寸都被选中 1 毫秒然后取消选中
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-29
      • 1970-01-01
      • 1970-01-01
      • 2018-09-27
      • 1970-01-01
      • 1970-01-01
      • 2018-08-26
      相关资源
      最近更新 更多