复合类的名字不被支持。考虑寻找一个类名和过滤结果 [英] Compound class names are not supported. Consider searching for one class name and filtering the results
问题描述
我使用driver.findelement by.classname方法来读取Firefox浏览器的元素,但我得到复合类的名字不被支持。考虑寻找一个类名和过滤的结果。例外
下面是我的代码
driver.FindElement(通过。 。类名(花鲢CRB))Text.Trim()的ToString()
//这里是浏览器的HTML的样子
<表单动作=#ID =aspnetForm的onsubmit =返回false;>
<节ID =LX-家的风格=保证金底:50像素;>
< DIV CLASS =bigbanner>
< DIV CLASS =扑通MC>
< DIV CLASS =花鲢CRB>铅交货时EASY< / DIV>
< / DIV>
< / DIV>
< /节>
< /表及GT;
没有,你自己的答案是不是最好的。在你的问题而言
假设你有HTML这样的:
< DIV CLASS =花鲢向右>铅交货时硬LT; / DIV>
< DIV CLASS =花鲢CRB>铅交货时EASY< / DIV>
driver.FindElement(By.ClassName(花鲢))
将找到并返回第一个 DIV
,而不是一个你想。你真正想要的是像 driver.FindElement(By.ClassName(花鲢CRB))
,但就像你在你的问题说,这将无法工作,你需要另辟蹊径,找到复合类名的元素。
这就是为什么大多数人使用更强大的 By.CssSelector
或 By.XPath
。然后你有:
CssSelector(最好的):
驱动程序.FindElement(By.CssSelector(bighead.crb。)); //灵活搭配小鳙鱼CRB,花鲢CRB,CRB花鲢,等等。
driver.FindElement(By.CssSelector([*类='鳙鱼CRB'])); //秩序问题,比赛类包含花鲢CRB
driver.FindElement(By.CssSelector([CLASS ='鳙鱼CRB'])); //匹配花鲢CRB严格
的XPath(越好):
driver.FindElement(By.XPath(.//* [含有(@class,花鲢),包含(@class,CRB) ])); //灵活搭配小鳙鱼CRB,花鲢CRB,CRB花鲢,等等。
driver.FindElement(By.XPath(.//* [含有(@class,花鲢CRB )])); //秩序问题,比赛类包含字符串花鲢CRB只有
driver.FindElement(By.XPath(.//*[@类='鳙鱼CRB'])); //匹配类字符串花鲢CRB严格
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 class name and filtering the results." exception
here is my code
driver.FindElement(By.ClassName("bighead crb")).Text.Trim().ToString()
//and here is how the html of browser looks like
<form action="#" id="aspnetForm" onsubmit="return false;">
<section id="lx-home" style="margin-bottom:50px;">
<div class="bigbanner">
<div class="splash mc">
<div class="bighead crb">LEAD DELIVERY MADE EASY</div>
</div>
</div>
</section>
</form>
No, your own answer isn't the best one in terms of your question.
Imagine you have HTML like this:
<div class="bighead ght">LEAD DELIVERY MADE HARD</div>
<div class="bighead crb">LEAD DELIVERY MADE EASY</div>
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
这篇关于复合类的名字不被支持。考虑寻找一个类名和过滤结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!