使用LinkText一次搜索2件事

时间:2018-06-04 15:19:00

标签: c# selenium webdriver

我正在制作一个“项目查找器”,对于最高级的网站,我的代码可以点击这件衣服。但它只是点击行中的第一项 - > it picks the pink one, because its the first in the row

代码

    {
        IWebDriver driver = new ChromeDriver();
        driver.Url = "http://www.supremenewyork.com/shop/all/jackets";

        driver.Manage().Window.Maximize();

        driver.FindElement(By.LinkText("Polka Dots Rayon Work Jacket")).Click();

}

我怎么能这样做,所以它也会搜索它的颜色,所以它会点击夹克,例如,还有颜色?

外套名称的HTML代码:<a class="name-link" href="/shop/jackets/que2rcwml/qft89kxy0">Polka Dots Rayon Work Jacket</a>

夹克颜色的HTML代码:<a class="name-link" href="/shop/jackets/que2rcwml/qft89kxy0">Light Pink</a>

1 个答案:

答案 0 :(得分:0)

您可以使用XPath来获得所需,例如

//article[.//a[text()='Black']]会找出颜色为Black的衣服,

现在我们需要添加限制,以便从以上所有黑色衣服上按名称找到想要的衣服:

//article[.//a[text()='Black']]//a[text()='Polka Dots Rayon Work Jacket']

IWebDriver driver = new ChromeDriver();

driver.Url = "http://www.supremenewyork.com/shop/all/jackets";

driver.Manage().Window.Maximize();

var color = "Black"
var clothe = "Polka Dots Rayon Work Jacket" 
var xpath = string.format("//article[.//a[text()='{0}']]//a[text()='{1}']", color, clothe)

driver.FindElement(By.XPath(xpath)).Click();