简体   繁体   中英

Search for 2 things at once with LinkText

I'm making an "item finder", for supreme website, and my code works to click on the piece of clothe. But it just clicks on the first item in the row -> it picks the pink one, because its the first in the row

Code

    {
        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();

}

How could i do, so it also takes in its search the color, so it will click on the jacket for example, with the color aswell?

the HTML code of the jacket name: <a class="name-link" href="/shop/jackets/que2rcwml/qft89kxy0">Polka Dots Rayon Work Jacket</a>

the HTML code of the jacket color: <a class="name-link" href="/shop/jackets/que2rcwml/qft89kxy0">Light Pink</a>

You can use XPath to get you want, for example

//article[.//a[text()='Black']] will find out clothes which color is Black ,

Now we need to add restriction to find wanted clothe by name from above all Black clothes as following:

//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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM