在我的网页中,有div一个class名为Test.
我怎么能找到它XPath?
我想只选择一个名为.date的类
出于某种原因,我不能让这个工作.如果有人知道我的代码有什么问题,我将不胜感激.
@$doc = new DOMDocument();
@$doc->loadHTML($html);
$xml = simplexml_import_dom($doc); // just to make xpath more simple
$images = $xml->xpath('//[@class="date"]');
foreach ($images as $img)
{
echo $img." ";
}
Run Code Online (Sandbox Code Playgroud) 可能重复:
XPath:如何匹配包含特定字符串的属性
我正在尝试使用PHP DOM Xpath解析一些html,但是当元素有多个时,我在搜索类名时遇到问题.我发现如果我使用像孔属性值那样
$xpath->query('//div[@class="precodet precodeturgente"]');
Run Code Online (Sandbox Code Playgroud)
它有效,但如果我这样做
$xpath->query('//div[@class="precodet"]');
Run Code Online (Sandbox Code Playgroud)
它不会给我节点值.有时只有一个班级,其他班级不止一个班级,所以我想知道的是有没有办法搜索单个班级名称.
这是我试图使用的XPath:
//div[contains(@class='xyz ng-binding ng-scope') and not(contains(@class = 'ng-hide'))]
Run Code Online (Sandbox Code Playgroud)
我不确定这是什么正确的语法.基本上HTML看起来像:
class="xyz ng-binding ng-scope typeA ng-hide"
class="xyz ng-binding ng-scope typeB ng-hide"
Run Code Online (Sandbox Code Playgroud)
我想选择在HTML或者是情况typeA或者typeB,但没有ng-hide.
亲爱的Selenium Webdriver专家,
我想知道Selenium Webdriver中的字符串匹配方法是否与Java中的以下代码片段正常工作:
if (property.findElements(By.xpath("./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house']")).size() > 0 ) { // line 229
Run Code Online (Sandbox Code Playgroud)
以下是第229行读取的xhtml网页:
<dl class="cN-featDetails">
<dt class="proptype">Property type</dt>
<dd id="ctl00_ctl00_Content_Content_SrchResLst_rptResult_ctl01_EliteListingTemplate_ddPropertyType" class="propertytype type-house" title="Property type: House">House</dd>
Run Code Online (Sandbox Code Playgroud)
但是,这导致以下错误:
Address: 28B/171 Gloucester Street, Sydney
Exception in thread "main" org.openqa.selenium.InvalidSelectorException: The given selector ./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house'] is either invalid or does not result in a WebElement. The following error occurred:
[InvalidSelectorError] Unable to locate an element with the xpath expression ./dl[@class='cN-featDetails']/dd[matches(class,'propertytype type-house'] because of the following error:
[Exception... "The expression is not a …Run Code Online (Sandbox Code Playgroud) 我一直在搜索过去30分钟左右,但是似乎无法回答如何创建将与多个类匹配的xpath选择器。
阅读以下内容后:如何匹配包含某个字符串的属性?
我能找到的最接近的解决方案是:
//div[contains(@class,'atag') and contains(@class ,'btag')]
Run Code Online (Sandbox Code Playgroud)
但是,评论之一建议它也将匹配:
<div class="Patagonia Halbtagsarbeit">
Run Code Online (Sandbox Code Playgroud)
我应该使用哪个XPath选择器来选择具有多个类的div?
例:
<div class="fl badge bolded shadow">
Run Code Online (Sandbox Code Playgroud) 嗨,我试图通过其类名和它包含的文本来定位元素
<div class="fc-day-number">15</div>
Run Code Online (Sandbox Code Playgroud)
fc-day-number页面上有一堆不同的值,我需要一个例如15.
我做
driver.find_element_by_class_name("fc-day-content")
Run Code Online (Sandbox Code Playgroud)
但我也需要它等于15,我被困在这里,请帮忙.
我想在 HtmlPage 中找到包含“日期”一词的类的任何元素。
即我想匹配以下任何一项:
<div class = 'date'> August 13 2017 </div>
<span class = 'pubDate'> August 12 2017 </div>
<div class = 'datePublished'> August 10 2017 </div>
Run Code Online (Sandbox Code Playgroud)
为了完全匹配“日期”,我使用以下内容:
HtmlPage page;
List<HtmlDivision> date = page.getByXPath("//div[@class='date']");
System.out.println(date.get(0));
Run Code Online (Sandbox Code Playgroud)
哪个工作正常。
但是,我如何更改它(或者我应该使用其他什么),以便能够匹配具有包含单词 date(不区分大小写)的类名的任何元素?