在您的情况下,仅使用类名是不够的。
因此,您需要更多限制来缩小范围。下面的 可用于CSS选择器,其中1可能是最适合您的需求的选择器。
局限性
例:
driver.FindElement(By.CssSelector("#rightbar > .menu > li:nth-of-type(3) > h5"));
driver.FindElement(By.XPath("//*[@id='rightbar']/ul/li[3]/h5"));
局限性
例:
// note that By.CssSelector(".ban.hot") and //*[contains(@class, 'ban hot')] are different, but doesn't matter in your case
IList<IWebElement> hotBanners = driver.FindElements(By.CssSelector(".ban.hot"));
IWebElement banUsStates = hotBanners[3];
局限性
例:
driver.FindElement(By.XPath("//h5[contains(@class, 'ban hot') and text() = 'us states']"));
局限性
例:
driver.FindElement(By.XPath("(//h5[contains(@class, 'ban hot')])[3]"));
局限性
例:
driver.FindElement(By.XPath(".//li[.//ul/li/a[contains(@href, 'geo.craigslist.org/iso/us/al')]]/h5"));