java - 逆向工程 WebElement 的 XPath

标签 java css regex xpath

我想知道如果其中一个属性已知但路径未知,是否有可能对 WebElement 的 XPath 进行逆向工程。例如,假设某个 WebElement 的 XPath 是 (@id,'vi_main_img_fs_thImg')]/img 但这是未知的。但是我知道这个 WebElement 将返回 http://myimage.com/72hjf89 (即唯一值)在调用 WebElement.getAttribute("src") 时 - 是否可以使用该信息对原始 XPath 值进行“逆向工程”?也许使用正则表达式之类的?

注意:.getAttribute("src") 只是一个例子,我想无论使用什么属性,只要这个值在页面上出现一次,无论使用什么方法,任何方法都可以工作。

有什么想法吗?

更新:

为了更详细地说明我正在尝试实现的目标,我在下面复制了 HTML 代码。

我正在查看的页面是:http://www.ebay.com/itm/111488868372

页面上的 HTML 是:

<document>
<html lang="en" xmlns:og="http://opengraphprotocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml" webdriver="true">
<head>
<body class=" vi-contv2 lhdr-ie- vi-hd-ops " style="background-image: url("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAAKCAYAAAB10jRKAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADBJREFUeNpEyqERADAIBMGbbwSH+qhA/6URXMyqBUhFBLIP6ip0ezk2zExqC58nwACVZwX67tO41gAAAABJRU5ErkJggg=="), url("http://ir.ebaystatic.com/pictures/aw/pics/cmp/ds3/imgbg.jpg"); background-repeat: repeat-x, repeat; background-position: 0px 30px, 0px 0px;">
<div id="gh-gb" tabindex="-1"/>
<!-- Default filmstrip js used with main image -->
<!-- adding filmstrip js used in main pic and with images carousel -->
<div id="Head"/>
<div id="Body" class=" sz940 " itemtype="http://schema.org/Product" itemscope="itemscope">
<div id="TopPanelDF">
<div id="CenterPanelDF">
<div id="CenterPanel" class=" ebaylocale_en_US ebay_longlngsite ">
<style> /* PicturePanel */ #PicturePanel div.img { border:1px solid #ccc; background-color:white; } /* BuyBox */ .actPanel  { border-top:1px solid #ccc; border-left:1px solid #ccc; border-right:1px solid #ccc; } .watchListCmp { border-bottom:1px solid #ccc; border-left:1px solid #ccc; border-right:1px solid #ccc; } </style>
<h3 class="vi-inheritstyl">
<div id="vi_sme_prmts_bnr_cntr1" class="vi-sme-ss-v1">
<!-- Placement 100005 -->
<!-- Placement 100011 && 100012 -->
<div id="CenterPanelInternal" class="">
<div id="PicturePanel" class="pp-c">
<h3 class="g-hdn">Picture Information</h3>
<div class="pp-ic pp-ic300">
<div class="l-shad lftd img img300">
<table class="img img300">
<tbody>
<tr>
<td class="img img300">
<div id="test">
<script type="text/javascript">var sPT = new Date().getTime(),picTimer,picTimer1,picTimer2;</script>
<a id="linkMainImg" style="display: block; cursor: default; text-decoration: none;" href="javascript:;">
<div id="mainImgHldr" class="" title="Journeys-Edge-Deluxe-Manicure-Set-with-Deluxe-Carrying-Case" style="width: 300px; background-image: none; cursor: pointer;">
<!-- <span id="mainImgHldr" style="display: inline-block;"> -->
<img id="icThrImg" class="img img300 vi-hide-mImgThr" alt="Image is loading" imgsel="0" src="http://ir.ebaystatic.com/pictures/aw/pics/globalAssets/imgLoading_30x30.gif" style="display:none;"/>
<img id="icImg" class="img img300" alt="Journeys-Edge-Deluxe-Manicure-Set-with-Deluxe-Carrying-Case" clk="0" onload="picTimer=new Date().getTime();" style="" src="http://i.ebayimg.com/images/g/eHAAAOSwLzdWS4C8/s-l300.jpg" itemprop="image" mskuskip="false"/>
<!-- </span> -->
</div>
</a>

“src”值为:http://i.ebayimg.com/images/g/eHAAAOSwLzdWS4C8/s-l300.jpg (这是唯一已知的值)

我试图用这种方法获得的值(ID)是:

.//*[@id='PicturePanel']/div[1]/div[1]/table/tbody/tr/td/a/div/img[2](这是元素的 XPath)

最佳答案

您可以只使用不同的 XPATH。

id(通常)是唯一的,因此使用 id 属性查找元素更有意义。

更新: 您可以使用如下所示的 XPATH 检索 ID:

//img[@src = "http://i.ebayimg.com/images/g/eHAAAOSwLzdWS4C8/s-l300.jpg"]/string(@id)

如果结果有多个元素,您将得到一个序列。

关于java - 逆向工程 WebElement 的 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34195017/

相关文章:

java - 仅在由某些字符分隔的文本的某些部分替换子字符串

java - 我如何在 Java 中获取打印机的品牌和型号?

java - 清除 Java 中的 Singleton 实例

java - 在 Java 中将 ISO 时间戳转换为 UTC

css - 使用 HTML/CSS 在响应式网格上的图像上叠加文本?

html - 带填充的边框垂直或水平中心的图像

html - 背景图像 1px 偏移

swift - 匹配字符串中除特定模式外的所有子字符串

java - findViewById() 下仅显示一个 ImageButton

c# - .Net字符串解析库,或解析.Net代码文件的regex