我使用Selenium来获取DOM元素。我有点混淆了使用xpath的双点。
字面意思是:
..(双点)-这将选择当前节点的父节点。
此外,我还查看了解释“使用双点(.)”的文章,如:
假设您有一个表单,您需要在其中键入电子邮件id。要查找电子邮件文本框,首先需要搜索电子邮件标签,然后再返回到输入节点键入。
例如,: //label[text()=’Email’]/../input
然后,我试图在DOM用例中使用相同的逻辑来使用..:
<li class="b-online-edit b-product-gallery__item js-rtb-partner" data-qaid="product-block" data-product-id="97423911" data-tg-chain="{"view_type": "preview"}">
<a class="b-online-edit__link" data-edit-role="productInfo" data-edit-id="97423911" title="Редактировать товар" style="display:none"></a>
<a class="b-online-edit__horizontal-borders" data-edit-role="productInfo" data-edit-id="97423911" style="display:none"></a>
<a class="b-online-edit__vertical-borders" data-edit-role="productInfo" data-edit-id="97423911" style="display:none"></a>
<div class="b-product-gallery__sku" title="Код:">
<span title="A2VG0Y0">A2VG0Y0</span>
</div>
<a class="b-product-gallery__image-link" href="..." title="DU-104, фотобарабан, Drum Unit, Konica Minolta Bizhub C6000 C7000" data-tg-became-visible="[{"ns": "ontheio", "method": "pageviewsProduct", "args": [{}], "payloads": {"oi_common": 97423911}, "uuid": "61796d2f952eaa08f1d5a4a94125f864f0899cf8e7e3e470b25b382432ff0d3b"}]" data-tg-clicked="[{"ns": "ontheio", "method": "productClick", "args": [{}], "payloads": {"oi_common": 97423911}, "uuid": "e57184c4caed5461dd02a34817916b6351bbdb4f7c2b6f340368df85678d4ef6"}]">
<img class="b-product-gallery__image" src="https://images.ua.prom.st/178769613_w200_h200_du-104-fotobaraban-drum.jpg" alt="DU-104, фотобарабан, Drum Unit, Konica Minolta Bizhub C6000 C7000"/>
<i class="b-product-gallery__no-image"></i>
</a>
<a class="b-product-gallery__title" href="..." id="link_to_product_97423911" data-tg-clicked="[{"ns": "ontheio", "method": "productClick", "args": [{}], "payloads": {"oi_common": 97423911}, "uuid": "e57184c4caed5461dd02a34817916b6351bbdb4f7c2b6f340368df85678d4ef6"}]">DU-104, фотобарабан, Drum Unit, Konica Minolta Bizhub C6000 C7000</a>
<div class="b-product-gallery__prices">
<span class="b-product-gallery__current-price">6 690 <span class="notranslate">грн.</span></span>
</div>
<div class="b-product-gallery__data">
<span class="b-product-gallery__state">Ожидается</span>
<i class="b-product-gallery__data-hider"></i>
</div>使用代码片段:
driver.findElement(By.xpath("//*[contains(text(),'A2VG0Y0')]/../../a[5]"))
driver.findElement(By.xpath("//*[contains(text(),'DU-104, ... C6000 C7000')]/../div[1]/span"))
它工作得很好,但我想完全理解,如果..与父级相关,是否可以显式地将..更改为父级?
因为如果我改变了,比如:
driver.findElement(By.xpath("//*[contains(text(),'DU-104')]/ul/li[2]/div[1]/span"))
或
driver.findElement(By.xpath("//*[contains(text(),'DU-104')]/li[2]/div[1]/span"))
它不起作用。
..是否是隐式定义方法包含()的父类?我可以通过OR操作符将这两个查询组合起来吗?
发布于 2019-08-06 19:25:48
正如您所看到的,用/../代替/li[2]/是行不通的。如果不使用..,则假定您正在向下移动DOM。您可能可以想象一个场景,在当前元素的上方和下面可能有一个/li[2] .XPath解析器如何能够确定要移动的方向?
还有一种方法可以创建XPath定位器,在不使用..的情况下查找这些元素。与使用..的定位器相比,它们应该不那么脆弱(容易崩溃),但是如果提供了HTML,则可以获得相同的结果。
考虑到所提供的HTML,我假设产品名称是"DU-104,фотобарабан,Drum Unit,Konica Minolta Bizhub C7000“,产品代码是"A2VG0Y0”。如果我想错了,你要么让我知道,我可以修理标签,要么在你读的时候在你脑子里重命名.:)
如果希望泛型定位器从产品代码中查找产品名称,则可以使用以下内容。
//li[contains(@class,'b-product-gallery__item')][.//span[@title='A2VG0Y0']]//a[@class='b-product-gallery__title']
^ find an LI tag that contains that specific class
^ that has a descendant SPAN that contains the desired product code
^ where the LI has a descendant A that contains the specific class我将它封装在一个函数中,这样它就可以重用了,您只需传递产品代码并返回标题
public String getProductNameFromProductCode(String productCode)
{
return driver.findElement(By.xpath("//li[contains(@class,'b-product-gallery__item')][.//span[@title='" + productCode + "']]//a[@class='b-product-gallery__title']")).getText();
}把它叫做
String productName = getProductNameFromProductCode("A2VG0Y0");如果要使用产品名称并获取产品代码,可以使用下面的定位器
//li[contains(@class,'b-product-gallery__item')][.//a[.='DU-104, фотобарабан, Drum Unit, Konica Minolta Bizhub C6000 C7000']]//span[@title]
^ find an LI tag that contains the specific class
^ that has a descendant A that contains the product name
^ where the LI has a descendant SPAN that has a title attribute把这个定位器放到一个可重用的函数中.
public String getProductCodeFromProductName(String productName)
{
return driver.findElement(By.xpath("//li[contains(@class,'b-product-gallery__item')][.//a[.='" + productName + "']]//span[@title]")).getText();
}把它叫做
String productCode = getProductCodeFromProductName("DU-104, фотобарабан, Drum Unit, Konica Minolta Bizhub C6000 C7000");https://stackoverflow.com/questions/57381614
复制相似问题