首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >硒双点法的应用

硒双点法的应用
EN

Stack Overflow用户
提问于 2019-08-06 17:56:44
回答 1查看 856关注 0票数 0

我使用Selenium来获取DOM元素。我有点混淆了使用xpath的双点。

字面意思是:

.. (双点)-这将选择当前节点的父节点。

此外,我还查看了解释“使用双点(.)”的文章,如:

假设您有一个表单,您需要在其中键入电子邮件id。要查找电子邮件文本框,首先需要搜索电子邮件标签,然后再返回到输入节点键入。

例如, //label[text()=’Email’]/../input

然后,我试图在DOM用例中使用相同的逻辑来使用..

代码语言:javascript
复制
<li class="b-online-edit b-product-gallery__item  js-rtb-partner" data-qaid="product-block" data-product-id="97423911" data-tg-chain="{&#34;view_type&#34;: &#34;preview&#34;}">
    <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="[{&#34;ns&#34;: &#34;ontheio&#34;, &#34;method&#34;: &#34;pageviewsProduct&#34;, &#34;args&#34;: [{}], &#34;payloads&#34;: {&#34;oi_common&#34;: 97423911}, &#34;uuid&#34;: &#34;61796d2f952eaa08f1d5a4a94125f864f0899cf8e7e3e470b25b382432ff0d3b&#34;}]" data-tg-clicked="[{&#34;ns&#34;: &#34;ontheio&#34;, &#34;method&#34;: &#34;productClick&#34;, &#34;args&#34;: [{}], &#34;payloads&#34;: {&#34;oi_common&#34;: 97423911}, &#34;uuid&#34;: &#34;e57184c4caed5461dd02a34817916b6351bbdb4f7c2b6f340368df85678d4ef6&#34;}]">
        <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="[{&#34;ns&#34;: &#34;ontheio&#34;, &#34;method&#34;: &#34;productClick&#34;, &#34;args&#34;: [{}], &#34;payloads&#34;: {&#34;oi_common&#34;: 97423911}, &#34;uuid&#34;: &#34;e57184c4caed5461dd02a34817916b6351bbdb4f7c2b6f340368df85678d4ef6&#34;}]">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&nbsp;<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>

使用代码片段:

  • 要通过代码获得标题"DU-104,. C6000 C7000":

driver.findElement(By.xpath("//*[contains(text(),'A2VG0Y0')]/../../a[5]"))

  • 要按标题获得代码"A2VG0Y0":

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操作符将这两个查询组合起来吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-08-06 19:25:48

正如您所看到的,用/../代替/li[2]/是行不通的。如果不使用..,则假定您正在向下移动DOM。您可能可以想象一个场景,在当前元素的上方和下面可能有一个/li[2] .XPath解析器如何能够确定要移动的方向?

还有一种方法可以创建XPath定位器,在不使用..的情况下查找这些元素。与使用..的定位器相比,它们应该不那么脆弱(容易崩溃),但是如果提供了HTML,则可以获得相同的结果。

考虑到所提供的HTML,我假设产品名称是"DU-104,фотобарабан,Drum Unit,Konica Minolta Bizhub C7000“,产品代码是"A2VG0Y0”。如果我想错了,你要么让我知道,我可以修理标签,要么在你读的时候在你脑子里重命名.:)

如果希望泛型定位器从产品代码中查找产品名称,则可以使用以下内容。

代码语言:javascript
复制
//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

我将它封装在一个函数中,这样它就可以重用了,您只需传递产品代码并返回标题

代码语言:javascript
复制
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();
}

把它叫做

代码语言:javascript
复制
String productName = getProductNameFromProductCode("A2VG0Y0");

如果要使用产品名称并获取产品代码,可以使用下面的定位器

代码语言:javascript
复制
//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

把这个定位器放到一个可重用的函数中.

代码语言:javascript
复制
public String getProductCodeFromProductName(String productName)
{
    return driver.findElement(By.xpath("//li[contains(@class,'b-product-gallery__item')][.//a[.='" + productName + "']]//span[@title]")).getText();
}

把它叫做

代码语言:javascript
复制
String productCode = getProductCodeFromProductName("DU-104, фотобарабан,  Drum Unit,  Konica Minolta Bizhub C6000 C7000");
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57381614

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档