[英]How to extract xml:lang value using Selenium
I have the HTML tag like: 我有这样的HTML标记:
html xmlns = " http://www.w3.org/1999/xhtml " xml:lang=' en-US ' lang='en-US '
By using selenium I am getting value of lang attribute, but while retrieving value of xml:lang getting null. 通过使用硒,我得到了lang属性的值,但是在检索xml:lang的值时却得到了null。
htmlTag.getAttribute("lang"); //en-US
htmlTag.getAttribute("xml:lang"); //null
Does anyone knows how to get value of xml:lang
? 有谁知道如何获取xml:lang
值?
It should work if this attribute presents and return null
if it's not there. 如果存在此属性,它应该可以工作;如果不存在,则返回null
。 As the name indicates, this attribute gives directives for XML documents, you wouldn't find it in the majority of the websites (eg stackoverflow, google, facebook). 顾名思义,此属性为XML文档提供了指令,在大多数网站(例如stackoverflow,google,facebook)中都找不到。 Normally you should retrieve it the same way you do for the rest of attributes, eg: 通常,您应该以与其余属性相同的方式检索它,例如:
String xmlLang = driver.findElement(By.tagName("html")).getAttribute("xml:lang");
You can check if an attribute exists with the following method: 您可以使用以下方法检查属性是否存在:
private static boolean attributeExists(WebElement elem, String attr) {
return elem.getAttribute(attr) != null;
}
Note that for HTML pages, Selenium does not return null
for a missing lang
attribute, but it returns an empty String
. 请注意,对于HTML页面,Selenium不会为缺少的lang
属性返回null
,而是返回一个空的String
。 For instance, at the time I am writing this post, StackOverflow.com does not include the lang
and xml:lang
in its served html-source. 例如,在撰写本文时, StackOverflow.com在其提供的html源代码中未包含lang
和xml:lang
。
But, Selenium will return null
for xml:lang
and an empty String
for lang
; 但是,硒将返回null
的xml:lang
和一个空String
为lang
; thus, as a false-positive case, the aforementioned method attributeExists
will return true
for a non-existing lang
attribute. 因此,作为一个假阳性的情况,上述方法attributeExists
将为不存在的lang
属性返回true
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.