Compound class names are not supported. Consider searching for one class name and filtering the results

后端 未结 2 1549
说谎
说谎 2020-12-03 18:55

I am using driver.findelement by.classname method to read an element on firefox browser but i am getting \"Compound class names are not supported. Consider searching for one

2条回答
  •  时光取名叫无心
    2020-12-03 19:31

    No, your own answer isn't the best one in terms of your question.

    Imagine you have HTML like this:

    LEAD DELIVERY MADE HARD
    LEAD DELIVERY MADE EASY

    driver.FindElement(By.ClassName("bighead")) will find both and return you the first div, instead of the one your want. What you really want is something like driver.FindElement(By.ClassName("bighead crb")), but like you said in your question, this won't work as you need another way to find elements by compound class names.

    This why most people use more powerful By.CssSelector or By.XPath. Then you have:

    CssSelector (the best):

    driver.FindElement(By.CssSelector(".bighead.crb")); // flexible, match "bighead small crb", "bighead crb", "crb bighead", etc.
    driver.FindElement(By.CssSelector("[class*='bighead crb']")); // order matters, match class contains  "bighead crb"
    driver.FindElement(By.CssSelector("[class='bighead crb']")); // match "bighead crb" strictly
    

    XPath (the better):

    driver.FindElement(By.XPath(".//*[contains(@class, 'bighead') and contains(@class, 'crb')]")); // flexible, match "bighead small crb", "bighead crb", "crb bighead", etc.
    driver.FindElement(By.XPath(".//*[contains(@class, 'bighead crb')]")); // order matters, match class contains string "bighead crb" only
    driver.FindElement(By.XPath(".//*[@class='bighead crb']")); // match class with string "bighead crb" strictly
    

提交回复
热议问题