2015-12-10 48 views
0

我想知道是否有可能對WebElement的XPath進行反向工程,如果其中一個屬性已知,但路徑未知。例如,假設某個WebElement的XPath是(@ id,'vi_main_img_fs_thImg')]/img,但這是未知的。然而,我知道當調用WebElement.getAttribute(「src」)時,這個WebElement將返回http://myimage.com/72hjf89(即唯一值) - 是否可以使用這些信息來「反向工程」原始XPath值?也許使用正則表達式或其他?WebElement的反向工程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> 

「源」 值: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](這個我s XPath到元素)

+0

我不明白你的問題。 –

回答

0

您可以使用不同的XPATH。

該id將(通常)是唯一的,因此使用id屬性查找元素更有意義。

UPDATE: 您可以通過使用像這樣的XPATH檢索ID:

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

在情況下,結果有一個以上的元素,你會得到一個序列。

+0

對不起,但我試過這個,它說XPath是無效的。假設是在http部分的雙引號?我在哪裏可以瞭解更多關於這個語法 –

+0

選擇此項開始:http://www.w3schools.com/xsl/xpath_syntax.asp 您能否提供一個來自您嘗試解析的HTML的例子,以便我可以給出更準確的答案? –

+0

我更新了這個問題.. –