简体   繁体   English

WebElement的反向工程师XPath

[英]Reverse Engineer XPath of WebElement

I was wondering if it's possible to reverse engineer the XPath of a WebElement if one of the attributes are known but the path is unknown. 我想知道是否可以对WebElement的XPath进行反向工程,如果其中一个属性已知但路径未知。 For example, suppose the XPath to a certain WebElement is (@id,'vi_main_img_fs_thImg')]/img but this is not known. 例如,假设某个WebElement的XPath是(@ id,'vi_main_img_fs_thImg')] / img,但这是未知的。 I know however that this WebElement will return http://myimage.com/72hjf89 (ie, a unique value) when calling WebElement.getAttribute("src") - is it possible to use the information to "reverse engineer" the original XPath value? 但是,我知道该WebElement在调用WebElement.getAttribute(“ src”)时将返回http://myimage.com/72hjf89 (即唯一值)-是否有可能使用该信息对原始XPath进行“反向工程”值? Perhaps using Regex or something? 也许使用正则表达式之类的东西?

Note: The .getAttribute("src") was only an example, I suppose whatever method works will work no matter what attribute is used as long as this value occurs once on page. 注意:.getAttribute(“ src”)只是一个示例,我想只要该值在页面上出现一次,无论使用哪种属性,无论哪种方法都可以使用。

Any ideas? 有任何想法吗?

Update: 更新:

To give more detailed example of what I'm trying to achieve I'm copying HTML code below. 为了更详细地说明我要实现的目标,我在下面复制HTML代码。

The page I am looking at is: http://www.ebay.com/itm/111488868372 我正在查看的页面是: http : //www.ebay.com/itm/111488868372

The HTML on page is: 页面上的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>

The "src" value is: http://i.ebayimg.com/images/g/eHAAAOSwLzdWS4C8/s-l300.jpg (this is the only known value) “ src”值是: http : //i.ebayimg.com/images/g/eHAAAOSwLzdWS4C8/s-l300.jpg (这是唯一的已知值)

The value (ID) I am trying to obtain with this method is: 我试图通过此方法获取的值(ID)为:

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

You can just use a different XPATH. 您可以只使用其他XPATH。

The id will (usually) be unique, so it makes more sense to look for an element using the id attribute. id通常是唯一的,因此使用id属性查找元素更有意义。

UPDATE: You can retrieve the ID by using an XPATH that looks like this: 更新:您可以通过使用如下所示的XPATH来检索ID:

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

In case the result has more than one elements, you will get a sequence. 如果结果包含多个元素,您将获得一个序列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM