我有以下 xml 摘录,我想要的只是版本标记中的字符串 2014.4.0.0008。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExternalMetadataPack xmlns="xmlns://www.fortifysoftware.com/schema/externalMetadata" schemaVersion="1.0">
<PackInfo>
<Name>Main External List Mappings</Name>
<PackID>main-external-mappings</PackID>
<Version>2014.4.0.0008</Version>
</PackInfo>
</ExternalMetadataPack>
Run Code Online (Sandbox Code Playgroud)
我尝试了一些事情并得到了这样的结果:
xmllint --xpath "//PackInfo/@Version[local-name()='ExternalMetadataPack']" externalmetadata.xml
Run Code Online (Sandbox Code Playgroud)
但似乎没有任何效果。这表示 XPath 集是空的,如果我在 --xpath 参数中包含 string() ,那么它不会返回任何内容。
我试图从 Xpath 查询返回一些结果,但它不会正确选择元素。我正在使用以下代码:
public function getTrustPilotReviews($amount)
{
$trustPilotUrl = 'https://www.trustpilot.co.uk/review/purplegriffon.com';
$html5 = new HTML5;
$document = $html5->loadHtml(file_get_contents($trustPilotUrl));
$document->validateOnParse = true;
$xpath = new DOMXpath($document);
$reviewsDomNodeList = $xpath->query('//div[@id="reviews-container"]//div[@itemprop="review"]');
$reviews = new Collection;
foreach ($reviewsDomNodeList as $key => $reviewDomElement)
{
$xpath = new DOMXpath($reviewDomElement->ownerDocument);
if ((int) $xpath->query('//*[@itemprop="ratingValue"]')->item($key)->getAttribute('content') >= 4)
{
$review = [
'title' => 'Test',
'author' => $xpath->query('//*[@itemprop="author"]')->item($key)->nodeValue,
'date' => $xpath->query('//*[@class="ndate"]')->item($key)->nodeValue,
'rating' => $xpath->query('//*[@itemprop="ratingValue"]')->item($key)->nodeValue,
'body' => $xpath->query('//*[@itemprop="reviewBody"]')->item($key)->nodeValue,
];
$reviews->add((object) $review);
}
}
return $reviews->take($amount);
}
Run Code Online (Sandbox Code Playgroud)
此代码不会返回任何内容:
//div[@id="reviews-container"]//div[@itemprop="review"]
Run Code Online (Sandbox Code Playgroud)
但如果我把它改成:
//*[@id="reviews-container"]//*[@itemprop="review"]
Run Code Online (Sandbox Code Playgroud)
它部分有效,但不会返回正确的结果。
据我了解,Yang 模型中的“when”语句采用 XPATH 表达式作为其参数。
组合多个表达式以便对类型/值数据容器进行建模的正确 YANG XPATH 语法是什么?
container c1 {
leaf attrib-type {
type uint32;
}
leaf attrib-val-int {
when "../attrib-type = 1 or ../attrib-type = 2"
type uint32;
}
leaf attrib-val-string {
when "../attrib-type = 5 or ../attrib-type = 6"
type string;
}
}
Run Code Online (Sandbox Code Playgroud) 我必须使用 GuavaLoadingCache将 xpath 表达式缓存为 xpath 值。
问题在于并非所有 xpath 都有值。大多数时候该值为null.
代码片段:
private LoadingCache<String, List<String>> cachedXpaths = CacheBuilder.newBuilder()
.expireAfterWrite(3, TimeUnit.MINUTES)
.maximumSize(1000)
.concurrencyLevel(5)
.weakKeys()
.build(new CacheLoader<String, List<String>>() {
@Override
public List<String> load(String key) throws Exception {
return createListByKey(key);
}
});
private static List<String> createListByKey(String key) throws Exception {
List<String> values = null;
try {
values = instance.getXpathValues(key);
} catch (XPathExpressionException ignore) {
}
return values;
}
Run Code Online (Sandbox Code Playgroud)
结果:
testEncounterSection(com.epam.cdatest.section.EncountersSectionTest) Time elapsed: 0.002 sec <<< FAILURE!
com.google.common.util.concurrent.UncheckedExecutionException: com.epam.cdatest.exceptions.XpathHasEmptyValueException
at com.epam.cdatest.parsers.XpathEvaluator.getXpathValues(XpathEvaluator.java:123)
at …Run Code Online (Sandbox Code Playgroud) driver.findElement(By.xpath(".//div[contains(text(),'Approval Date')]")).click();
Run Code Online (Sandbox Code Playgroud)
上面的代码找到了表中的第一个元素。
表中有三行带有文本“批准日期”。
如何选择第三行而不是默认选择第一行?
我需要 xml 节点的完整路径。我看到了这个问题
的答案,但我无法使用它。
下面是我在 php web 测试器上使用但没有成功的代码:
$xml = <<<EOF
<root>
<First>
<Martha>Text01</Martha>
<Lucy>Text02</Lucy>
<Bob>
<Jhon>Text03</Jhon>
</Bob>
<Frank>One</Frank>
<Jessy>Two</Jessy>
</First>
<Second>
<Mary>
<Jhon>Text04</Jhon>
<Frank>Text05</Frank>
<Jessy>Text06</Jessy>
</Mary>
</Second>
</root>
EOF;
$MyXml = new SimpleXMLElement($xml);
$Jhons = $MyXml->xpath('//Jhon');
foreach ($Jhons as $Jhon){
echo (string) $Jhon;
//No one of the following works
echo (string) $Jhon->xpath('./node()/path()');
echo (string) $Jhon->xpath('./path()');
echo (string) $Jhon->xpath('.path()');
echo (string) $Jhon->path();
echo '<br/> ';
}
Run Code Online (Sandbox Code Playgroud)
我需要:“/root/First/Bob/Jhon”和“/root/Second/Mary/Jhon”
我需要使用SQL替换xml中的值,我面临的挑战是我要替换的值不在特定的xpath上,相同的值在不同的节点上并且xpath也不同,所以基本上我想找到并替换一个值。
例子:
<data>
<policy>
<Effectivedate>2018-04-05</Effectivedate>
<TermStartDate>2018-04-05</TermStartDate>
<Line>
<Risk>
<Coverage>
<Type>1</Type>
<coverstartdate>2018-04-05</coverstartdate>
</Coverage>
<Coverage>
<Type>2</Type>
<coverstartdate>2018-04-05</coverstartdate>
</Coverage>
<Coverage>
<Type>3</Type>
<coverstartdate>2018-04-05</coverstartdate>
</Coverage>
</Risk>
</Line>
</policy>
</data>
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,我需要将日期 2018-04-05 替换为 2018-04-06
请有人在这里帮忙。
我希望从下面所示的“listicle-page”类中的H2 (突出显示)元素获取值。目前,代码获取DIV元素中的所有值,而我只需要获取下面的类中包含的H2的值。
考虑以下 HTML:
请参阅下面的代码 -
private void getFact()
{
HtmlAgilityPack.HtmlWeb web = new HtmlAgilityPack.HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = web.Load("https://www.rd.com/culture/interesting-facts/");
var headerNames = doc.DocumentNode.SelectNodes("//div[@class='listicle-page']").ToList();
foreach(var item in headerNames)
{
MessageBox.Show(item.InnerText);
}
}
Run Code Online (Sandbox Code Playgroud) 我试图单击一个复选框,但它一直在单击“条款和条件”链接。虽然我的 xpath(下面提到)可以在最小化的窗口上工作,但是当窗口最大化时,它无法单击复选框,因为 href(图像)出现在复选框旁边的第二行中。寻找有关单击最大化窗口上的复选框小部件的一些建议。我需要集中注意力。
有趣的是,当我将鼠标悬停在 ::before (CSS 选择器)上时,只有小部件才会突出显示。
<div class="checkbox u-mar-bot-5">
<div class="checkbox__container">
<input class="checkbox__input" type="checkbox" id="basket-contact-terms" required data-parsley-multiple="basket-contact-terms" style>
<label class="checkbox__label checkbox__label--has-link checkbox__label--small" for="basket-contact-terms" style>
::before
"I have read and agree to " <a class="text-link text-link--base text-link- small" href="/terms-conditions" target="_blank">Terms and Conditions</a>
</label>
</div>
</div>
Run Code Online (Sandbox Code Playgroud)
图片:条款和条件
我尝试了几个选项,但始终无法选中该框,而是点击了“条款和条件”链接。我一定缺少一些基本的东西。
driver.findElement(By.xpath("//label[@for='basket-contact-terms']")).click();
driver.findElement(By.xpath("//label[contains(@class,'checkbox__label checkbox__label--has-link checkbox__label--small')]")).click();
Run Code Online (Sandbox Code Playgroud)
我确实环顾四周,发现有人建议使用这个(如下),所以我尝试了但没有成功:
WebElement elem = driver.findElement(By.xpath("//div[contains(@class,'checkbox u-mar-bot-5')]"));
Actions action = new Actions(driver);
action.moveToElement(elem).click().build().perform();
Run Code Online (Sandbox Code Playgroud)
任何建议将不胜感激。
我试图找出一种基于两个文本字符串使用 XPath 定位元素的方法。有问题的元素可以有文本 A 或文本 B,两者都是有效的,并且我需要根据该文本断言该元素是可见的。所以我需要一个像“//*...[contains(text(), 'A')] OR [contains(text(), 'B')]”这样的 XPath 表达式。这可能吗?这必须使用 XPath 1.0,因为它适用于 Selenium Webdriver。
我还没有找到任何方法来做到这一点,所以我目前正在检查 try-catch 中的第一个字符串,然后断言第二个字符串。