您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

需要通过CSS在selenium中找到元素

需要通过CSS在selenium中找到元素

在您的情况下,仅使用类名是不够的。

因此,您需要更多限制来缩小范围。下面的 可用于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"));
CSS 2022/1/1 18:13:52 有629人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶