栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

不支持复合类名。考虑搜索一个类名并过滤结果

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

不支持复合类名。考虑搜索一个类名并过滤结果

不,就您的问题而言,您自己的答案并不是最好的答案。

假设您有这样的HTML:

<div >LEAD DELIVERY MADE HARD</div><div >LEAD DELIVERY MADE EASY</div>

driver.FindElement(By.ClassName("bighead"))
会找到两个,然后返回您的第一个
div
,而不是您想要的一个。您真正想要的是
driver.FindElement(By.ClassName("bigheadcrb"))
,但是就像您在问题中说的那样,这将不起作用,因为您需要另一种通过复合类名称查找元素的方法。

这就是为什么大多数人使用功能更强大

By.CssSelector
By.XPath
。然后您有:

CssSelector(最好的):

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("[]")); // match "bighead crb" strictly

XPath(更好):

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" onlydriver.FindElement(By.XPath(".//*[@]")); // match class with string "bighead crb" strictly


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/616135.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号