所以,我面临的一个挑战是,该网页具有一种列表,其中某些元素的侧面带有文本和按钮,例如:
目标是通过引用特定文本来单击按钮(因为按钮的所有文本都是相同的-“按钮”),例如,仅单击“元素A”和“元素C”的按钮< / p>
我分析了HTML代码,尝试将xpath定位到我想要的按钮上有点复杂(至少对我而言)
从本质上讲,我将有一个变量将存储元素Text的字符串(“ Element A”,“ Element B” ...),然后单击链接到它们的按钮
HTML代码看起来像这样(那些字符串是类,仅供参考,但它们表示每个节点,例如div或span和嵌套的子代;对于混乱的绘制感到抱歉)
所以,我在这里迷路了,我试图寻找父轴和祖轴,但实际上我不太了解如何正确实现正确的xpath, 只是作为参考,尽管我确信这不是正确的语法,但我仍在尝试
// span [(text()='Element A')] //(查找同时包含Element Text和Element Button的主父对象)/(在具有该对象的父对象中查找“ Button”文本“元素A”)
问题在于,由于lsit上有很多元素,每个主父元素都是不同的,这就是为什么我使用元素Text(“元素A”,“元素B” ...)来定位特定的“按钮”
任何建议或建议都将受到高度赞赏! 也许有一些使用轴或谓词的技巧?
谢谢!
HTML页面的可操作部分,由于放置所有HTML代码并不实际,因此它非常庞大 例如,我所指的元素是:
然后,我需要根据我要定位的元素文本来定位所需的按钮(带有“共享”文本),例如:Hellblade Senua's Sacrifice
<div aria-busy="false" class="o8kakjsu role="listbox">
<div class="a8s20v7p k5wvi7nf buofh1pr pfnyh3mw l9j0dhe7 du4w35lb">
<div>
<div class="rq0escxv">
<div class="rq0escxv">
<div class="rq0escxv ">
<div class="cbu4d94t e5nlhep0 aodizinl">
<div class="buofh1pr">
<div class="dati1w0a">
<div class="j83agx80 ">
<div class="knvmm38d">
<h2 class=" " dir="auto">
<div class="l9j0dhe7 stjgntxs ni8dbmo4" style="display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2;">All</div>
</h2>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="">
<div class="muag1w35 b20td4e0">
<div data-vc-ignore-dynamic="1" style="padding-left: 8px; padding-right: 8px;">
<div class=" a8c37x1j">
<div class="">
<div class="">
<div class="">
<a aria-label="Senua's Saga Hellblade 2 ? Sacrifice" class="oajrlxb2" tabindex="0">
<div class="q9uorilb l9j0dhe7 pzggbiyp du4w35lb">
<svg class="" data-vc-ignore-dynamic="1" role="none" style="height: 40px; width: 40px;">
<mask id="jsc_c_4e">
<rect cy="20" fill="white" height="40" rx="8" ry="8" width="40" x="0" y="0"></rect>
</mask>
<g mask="url(#jsc_c_4e)">
<image x="0" y="0" height="100%"></image>
<rect class="mlqo0dh0 georvekb s6kb5r3f" cy="20" fill="white" height="40" rx="8" ry="8" width="40" x="0" y="0"></rect>
</g>
</svg>
</div>
</a>
</div>
</div>
<div class="ow4ym5g4 btwxx1t3 l9j0dhe7">
<div class="gs1a9yip">
<div class="" id="jsc_c_4d">
<div class="j83agx80 cbu4d94t ew0dbk1b irj2b8pg">
<div class="qzhwtbm6 knvmm38d"><span class="" dir="auto">Senua's Saga Hellblade 2 ? Sacrifice</span></div>
<div class="qzhwtbm6 knvmm38d"><span class="" dir="auto">Group</span></div>
</div>
</div>
</div>
<div class="bi6gxh9e ozuftl9m aov4n071 l9j0dhe7 o8rfisnq" aria-labelledby="jsc_c_4d">
<div class="bp9cbjyn j83agx80 btwxx1t3">
<div class="">
<div aria-label="Share" class="oajrlxb2 oo1teu6h " role="button" tabindex="0">
<div class="c4xchbtz by2jbhx6">
<div class="rq0escxv l9j0dhe7 "><span class="oi732d6d " dir="auto">Share</span></div>
</div>
<div class="" data-novc="1"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div data-vc-ignore-dynamic="1" style="padding-left: 8px; padding-right: 8px;">
<div class="ue3kfks5 pw54ja7n uo3d90p7 l82x9zwi a8c37x1j">
<div class="scb9dxdr">
<div class="nqmvxvec tvfksri0 aov4n071 l9j0dhe7">
<div class="">
<a aria-label="Hellblade: Senua's Sacrifice & Senua's Saga" class="oajrlxb2 tabindex="0">
<div class=" du4w35lb">
<svg class="pzggbiyp" data-vc-ignore-dynamic="1" role="none" style="height: 40px; width: 40px;">
<mask id="jsc_c_4g">
<rect cy="20" fill="white" height="40" rx="8" ry="8" width="40" x="0" y="0"></rect>
</mask>
<g mask="url(#jsc_c_4g)">
<image x="0" y="0" height="100%" ></image>
<rect class="mlqo0dh0 georvekb s6kb5r3f" cy="20" fill="white" height="40" rx="8" ry="8" width="40" x="0" y="0"></rect>
</g>
</svg>
</div>
</a>
</div>
</div>
<div class="btwxx1t3 l9j0dhe7">
<div class="">
<div class="" id="jsc_c_4f">
<div class=" ew0dbk1b irj2b8pg">
<div class="" dir="auto">Hellblade: Senua's Sacrifice & Senua's Saga</span></div>
<div class="qzhwtbm6 knvmm38d"><span class="hzawbc8m" dir="auto">group</span></div>
</div>
</div>
</div>
<div class="bi6gxh9e ozuftl9m aov4n071 l9j0dhe7 o8rfisnq" aria-labelledby="jsc_c_4f">
<div class="bp9cbjyn j83agx80 btwxx1t3">
<div class="">
<div aria-label="Share" class=" tv7at329" role="button" tabindex="0">
<div class="">
<div class=""><span class=" g0qnabr5" dir="auto">Share</span></div>
</div>
<div class="" data-novc="1"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div data-vc-ignore-dynamic="1" style="padding-left: 8px; padding-right: 8px;">
<div class="ue3kfks5 pw54ja7n uo3d90p7 l82x9zwi a8c37x1j">
<div class="">
<div class="nqmvxvec j83agx80 cbu4d94t bi6gxh9e tvfksri0 aov4n071 l9j0dhe7">
<div class="">
<a aria-label="AutoHotkey" class="oajrlxb2" tabindex="0">
<div class="q9uorilb l9j0dhe7 pzggbiyp du4w35lb">
<svg class="pzggbiyp" data-vc-ignore-dynamic="1" role="none" style="height: 40px; width: 40px;">
<mask id="jsc_c_4i">
<rect cy="20" fill="white" height="40" rx="8" ry="8" width="40" x="0" y="0"></rect>
</mask>
<g mask="url(#jsc_c_4i)">
<image x="0" y="0" "></image>
<rect class="mlqo0dh0 georvekb s6kb5r3f" cy="20" fill="white" height="40" rx="8" ry="8" width="40" x="0" y="0"></rect>
</g>
</svg>
</div>
</a>
</div>
</div>
<div class="">
<div class=" du4w35lb rj1gh0hx f10w8fjw pybr56ya">
<div class="" id="jsc_c_4h">
<div class="j83agx80 cbu4d94t ew0dbk1b irj2b8pg">
<div class="qzhwtbm6 knvmm38d"><span class="" dir="auto">AutoHotkey</span></div>
<div class="" dir="auto">Public group</span></div>
</div>
</div>
</div>
<div class="" aria-labelledby="jsc_c_4h">
<div class="bp9cbjyn j83agx80 btwxx1t3">
<div class="">
<div aria-label="Share" class="oajrlxb2" role="button" tabindex="0">
<div class="">
<div class=""><span class="" dir="auto">Share</span></div>
</div>
<div class="" data-novc="1"></div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
答案 0 :(得分:1)
根据您的示例,这是您应该使用的。 “地狱之刃:塞努阿的牺牲与塞努阿的传奇”的示例:
//div[.="Hellblade: Senua's Sacrifice & Senua's Saga"]/following::span[.="Share"][1]
我们寻找包含字符串变量的元素,然后寻找包含按钮文本的第一个元素。
因此,这应该可以解决您的网页的问题:
//span[.="Element A"]/following::*[.="Button"][1]
答案 1 :(得分:0)
经过长时间的研究,我终于做到了! 观看本教程:https://www.youtube.com/watch?v=a6B6XYbviRI
从那里我可以更好地理解XPath逻辑以及如何应对挑战,最终Xpath就是这样:
//span[(text()="Element A")]/parent::div/parent::div/parent::div/parent::div/parent::div//*[(text()="Button")]
对于那些不了解Xpath的人来说,这就像获取起点(元素),然后从那里精确地导航(如父母或您的情况),最后得到目标元素
如果有人知道如何做得更好,请!注释掉!