xpath java html element siblings

时间:2017-07-13 09:24:40

标签: java html xpath

我正在尝试获取某些元素的值。我能够得到第一个但是“跟随兄弟”

WebElement mainelem = url.findElement(By.tagName("main"));
WebElement piclinks = mainelem.findElement(By.xpath("//a[starts-with(@href, '/p/')]"));

XPATH: //*[@id="react-root"]/section/main/article/div[1]/div/div[1]/div[1]/a

usage of my following sibling:
WebElement piclinks = mainelem.findElement(By.xpath("//a[starts-with(@href, '/p/')]/following-sibling::a"));

我在Java中遇到的错误是:

无效的选择器:无法找到元素

我一直在尝试很多东西来解决这个问题,但它没有用。

最后是html代码:

<section data-reactroot="" class="_tlwfg _8f735"><main class="_6ltyr _rnpza" role="main"><article class="_3n7ri"><header class="_1l53n"><h1 class="_totu9"><!-- react-text: 7448 -->#<!-- /react-text --><!-- react-text: 7449 -->funny<!-- /react-text --></h1><span class="_s53mj"><!-- react-text: 7451 --><!-- /react-text --><span class="_bkw5z">101,244,153</span><!-- react-text: 7453 --> posts<!-- /react-text --></span></header><div class="_5kftd"><h2 class="_sai17">Top posts</h2><div class="_nljxa"><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfHui8FLgi/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="lol" class="_icyx7" id="pImage_434" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c0.50.1080.1080/20065328_164651100746803_6799075315676086272_n.jpg"></div><!-- react-empty: 7463 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWeaZpylSXA/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Supreme×Louis Vuitton
#チェキ
#撮影
#Supreme
#LouisVitton
#トレーナー
#着させて頂きんか
#ゆーたろさん
#thankx
#顔芸モデル
#me
#kaogeimodel
#funny" class="_icyx7" id="pImage_435" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c135.0.810.810/19955663_1269626093146401_407055753111666688_n.jpg"></div><!-- react-empty: 7472 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfKUOtnYXR/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Gas helium memang bisa mengubah suara. makanya hisapnya harus sampai habis, biar suaranya lama berubah. Tapi nda begini ji juga..
.
Feat : @iksanbangsawan @fajaranzari .
Location : @crovia
.
MENTION Temanmu yg suka joget-joget.
.
#indovidgram @indovidgram  #IVGComedy #IVGStory 
#dagelan #dagelanvideo #dagelantivi #ngakakkocak #bikinrame  #narsistrans7 #maknews #kamuharustahu #makassarinfo" class="_icyx7" id="pImage_436" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e15/19955138_479168035759394_4757550632582250496_n.jpg"></div><!-- react-empty: 7481 --><div class="_ovg3g"></div></div><div class="_qihym"><div class="_cxj4a"><span class="_btsl3 _soakw coreSpriteVideoIconLarge">Video</span></div></div></a></div></div><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWeCwFBgmim/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="The word you're looking for is &quot;shambles.&quot; (Via @freerefills) #spoonfeed" class="_icyx7" id="pImage_437" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19985795_511230512581070_7725260939766792192_n.jpg"></div><!-- react-empty: 7494 --><div class="_ovg3g"></div></div><div class="_sppa1"><ul class="_m9z3c"><li class="_sjq6j _ajqft"><!-- react-text: 7976 --><!-- /react-text --><span>4,198</span><!-- react-text: 7978 --><!-- /react-text --><span class="_4aw90 _soakw coreSpriteHeartSmall">likes</span><!-- react-text: 7980 --><!-- /react-text --></li><li class="_qq2if"><!-- react-text: 7982 --><!-- /react-text --><span>120</span><!-- react-text: 7984 --><!-- /react-text --><span class="_fuu7c _soakw coreSpriteSpeechBubbleSmall">comments</span><!-- react-text: 7986 --><!-- /react-text --></li></ul></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfFBGMlv2v/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Ładnie wyszedłem?  Dostane like? ❤️ #zizej #tattoo #like #selfie #me #awesome #poznan #poland #foresttattoo #forearmtattoo #follow #youtube #sexy #gday #epic #vsco #lol #funny #cute" class="_icyx7" id="pImage_438" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19955019_488052818214189_8337665358542405632_n.jpg"></div><!-- react-empty: 7503 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfK2w2hF1H/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="KARAOKE MİKRAFON
FİYAT:120₺
SİPARİŞ WHATSAPP:0534-336-63-19
KAPIDA ÖDEME/KREDİ KARTI VEYA NAKİT 
2 YIL GARANTİ 
ÇİFT HOPARLÖR 
BLUETOOTH ÖZELLİĞİ
USB FLAŞH GİRİŞ DESTEKLİ
MÜZİK PARÇA DEĞİŞİMİ
BASS AYARI
VOLUME AYARI
ECHO AYARI
TREBLE AYARI
MÜZİK ORKESTRA AYARI
POWER BANK (TELEFON ŞARJ ÖZELLİĞİ) 
RENKLER: SİYAH - GOLD - PEMBE 
#komikcaps #komikvideo #video #ankara #bursa #antalya #gününfotosu #ilginç #ilginçfoto #eğlenceli #bağlama #funny #follow #followme #baglama #instagram #likeme #turkey #imparator #makara #beyazshow #gülmek #vineler #vine #vibes #vines #komikvideolar #kadın #hadise #mizah" class="_icyx7" id="pImage_439" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c76.0.927.927/19986198_1879501575634377_6354536738727133184_n.jpg"></div><!-- react-empty: 7512 --><div class="_ovg3g"></div></div></a></div></div><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfFqaxB9Mg/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="She's happy when she gets food  #lunch" class="_icyx7" id="pImage_440" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c0.135.1080.1080/19984998_114386345860502_2029808965793087488_n.jpg"></div><!-- react-empty: 7522 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfGsAhAScy/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Hep gülelim  #funny #lol #lmao #lmfao #TagsForLikes #hilarious #laugh #laughing #tweegram #fun #friends #photooftheday #friend #wacky #crazy #silly #witty #instahappy #joke #jokes #joking #epic #instagood #instafun #funnypictures #haha #humor" class="_icyx7" id="pImage_441" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19535321_174518429756674_2290073804858195968_n.jpg"></div><!-- react-empty: 7531 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfGkmNDfgd/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Follow this beautiful girl - @eattravellaugh
@eattravellaugh
@eattravellaugh
Featured by ❤️
 @_insta_selfie
.
.
.
#selfie #nofilter #webstagram #follow4follow #iphoneonly #instago #pretty #l4l #my #style #family #instacool #life #hair #instafollow #likeforlike #eyes #christmas #20likes #funny #colorful #sun #bored #look #lol #tweegram #all_shots #nice #cool" class="_icyx7" id="pImage_442" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19955067_207718129756354_5852653957465243648_n.jpg"></div><!-- react-empty: 7540 --><div class="_ovg3g"></div></div></a></div></div></div></div><h2 class="_sai17">Most recent</h2><div><div class="_nljxa"><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOmLfgcGl/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="#love #like #happy #smile #tbt #throwback #tagsforlike #men #menfashion #fashion #boy #handsome #sunglasses #funny #cute #goodevening #throwbackthursday #fitmen #fitness" class="_icyx7" id="pImage_443" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19985143_1944726462469632_2655387475194675200_n.jpg"></div><!-- react-empty: 7553 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOl6SlDZF/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="•F A S T • F O O D • **Sprinting away the sprinkles** #quilledesigns #quilling #quilled #paperquilling #quilledpaper #paper #paperart #papercraft #paperartist #quillingstrips #comicstrip #comics #puns #playonwords #donuts #sprinkles #fastfood #funny #donut #sprinting" class="_icyx7" id="pImage_444" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/20065944_151087882114276_504967203435577344_n.jpg"></div><!-- react-empty: 7562 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlxWg5ny/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Lmao, this is not what i expected at all... hahahaha, I'll try again soon :(
.
.
.
.
.
.
.
.
.
#animation #traditionalanimation #animatic #art #drawing #draw  #pencil  #motion #character #alien #kawaii #cute #stopmotion #disney #cute #funny #frame #animacion #tradicional #design #diseño #dibujo #drawings #drawing" class="_icyx7" id="pImage_445" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e15/c120.0.480.480/19955301_150281645530681_7822799075828301824_n.jpg"></div><!-- react-empty: 7571 --><div class="_ovg3g"></div></div><div class="_qihym"><div class="_cxj4a"><span class="_btsl3 _soakw coreSpriteVideoIconLarge">Video</span></div></div></a></div></div><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlrNjXI8/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="#shake #shakeit #shakeshack #shakeology #shakespeareinthepark #shaker#shakeitup #shakethatass #shakespeare #shakespearelives #shakespearesglobe #shakeologyaddict #funny#lol#lmao#date #indian #dubai #usa #ufc #boxing#muaythai" class="_icyx7" id="pImage_446" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/20065225_333634613734880_2244990821642272768_n.jpg"></div><!-- react-empty: 7584 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlqJguA_/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="I'm Lego Batman 
#lego #batman #funny #keychain" class="_icyx7" id="pImage_447" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19933458_702485879950599_7655346332546105344_n.jpg"></div><!-- react-empty: 7593 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlp8nI8G/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Love you❤️❤️#friend #friends #fun #TagsForLikes #funny #love #instagood #igers #friendship #party #chill #happy #cute #photooftheday #live #forever #smile #bff #bf #gf #best #bestfriend #lovethem #bestfriends #goodfriends #besties #awesome #memories #goodtimes #goodtime" class="_icyx7" id="pImage_448" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c135.0.810.810/19984263_412312419162449_1340519160630738944_n.jpg"></div><!-- react-empty: 7602 --><div class="_ovg3g"></div></div></a></div></div><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlpUA8Ac/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="#memes #memestagram #instafunny #funny #dankmeme" class="_icyx7" id="pImage_449" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19986053_1395318580545213_5434597534683103232_n.jpg"></div><!-- react-empty: 7612 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlaWl1yC/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="@danielle1of3 likes to joke that with all the money I spend @starbucks that we could be getting married at the Ritz, well now after getting my morning ice tea without my name and just my face drawn on my cup, I'm inclined to agree with her...#starbucks #barista #icetea #nycphotographer #nycphotography #nyc #brooklyn #facemask #art #drawing #draw #drawingoftheday #foodporn #foodies #foodlover #foodpic #foodstagram #fun #funny #refillplease #beard #model" class="_icyx7" id="pImage_450" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/20065802_1529194863806276_2312596664898027520_n.jpg"></div><!-- react-empty: 7621 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlGFFbUh/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="#beach #street #shore #sky #beauty
#Repost#amazing #tbt #instagraff #nature #funny #pretty #c4c #l4l #night #noir #Love #vew #green #tree #art #all #pics #photofy#hdr" class="_icyx7" id="pImage_451" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c0.135.1080.1080/19986201_326503507806798_6900092237808402432_n.jpg"></div><!-- react-empty: 7630 --><div class="_ovg3g"></div></div></a></div></div><div class="_myci9"><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOlBmBvy_/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="mEeeeeEe" class="_icyx7" id="pImage_452" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/19986090_1425807950830882_8070492657287168000_n.jpg"></div><!-- react-empty: 7640 --><div class="_ovg3g"></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOkyrgCZa/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="#spoof #funny #crazy #humour #jokes #vine #comedy" class="_icyx7" id="pImage_453" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e15/c80.0.480.480/19985991_107038813218184_2045495655236894720_n.jpg"></div><!-- react-empty: 7649 --><div class="_ovg3g"></div></div><div class="_qihym"><div class="_cxj4a"><span class="_btsl3 _soakw coreSpriteVideoIconLarge">Video</span></div></div></a></div><div class="_8mlbc _vbtk2 _t5r8b"><a href="/p/BWfOkjlgdSz/?tagged=funny"><div class="_22yr2"><div class="_jjzlb"><img alt="Every minute should be enjoyed and savored.

#fun #instagramers #socialenvy #shopstemdesigns&nbsp; #smile #pretty #followme #nature #lol #fishing #hair #sunset #swag #throwbackthursday #instagood #beach #friends #hot #funny #blue #life #art #photo #cool #ebookwormsclub #bestoftheday #clouds #chasinglight #love #me" class="_icyx7" id="pImage_454" src="https://scontent-frx5-1.cdninstagram.com/t51.2885-15/s320x320/e35/c236.0.608.608/19955382_1493628214028033_4371753069210238976_n.jpg"></div><!-- react-empty: 7661 --><div class="_ovg3g"></div></div></a></div></div></div><div class="_ikcuh"><div class="_4tedc"><iframe aria-hidden="true" class="_823eg" tabindex="-1"></iframe></div></div><a href="/explore/tags/funny/?max_id=1558028080856028339" class="_8imhp _glz1g">Load more</a></div></article></main><nav class="_onabe _5z3y6" role="navigation"><div class="_giw59"></div><div class="_fjpuc _sq03j"><div class="_6v8vp _erzi3"><div class="_df358"><div class="_jha5b"><div class="_om391"><a class="_1b8in _soakw coreSpriteDesktopNavLogoAndWordmark" href="/">Instagram</a></div></div><div class="_9pxkq _icv3j"><input class="_9x5sw _qy55y" placeholder="Search" value="" type="text"><div class="_t1y9a _98hun"><div class="_etslc _1rn91"><span class="_oqxv9 coreSpriteSearchIcon"></span><span class="_9ea4j">Search</span></div></div></div><div class="_nhei4"><div class="_pq5am"><div class="_7smet"><a class="_soakw _vbtk2 coreSpriteDesktopNavExplore" href="/explore/">Find People</a></div><div class="_7smet"><a href="#" class="_im3et _vbtk2 coreSpriteDesktopNavActivity"><span class="_soakw">Activity Feed</span></a></div><div class="_7smet"><a class="_soakw _vbtk2 coreSpriteDesktopNavProfile" href="/memeyouhard/">Profile</a></div></div></div></div></div></div></nav><footer class="_oofbn" role="contentinfo"><div class="_mhrsk _pcuq6" style="max-width: 935px;"><nav class="_p1gbi" role="navigation"><ul class="_fh0f2"><li class="_fw3ds"><a class="_c4mil" href="/about/us/">About us</a></li><li class="_fw3ds"><a class="_c4mil" href="https://help.instagram.com/">Support</a></li><li class="_fw3ds"><a class="_c4mil" href="http://blog.instagram.com/">Blog</a></li><li class="_fw3ds"><a class="_c4mil" href="https://instagram-press.com/">Press</a></li><li class="_fw3ds"><a class="_c4mil" href="/developer/">API</a></li><li class="_fw3ds"><a class="_c4mil" href="/about/jobs/">Jobs</a></li><li class="_fw3ds"><a class="_c4mil" href="/legal/privacy/">Privacy</a></li><li class="_fw3ds"><a class="_c4mil" href="/legal/terms/">Terms</a></li><li class="_fw3ds"><a class="_c4mil" href="/explore/locations/">Directory</a></li><li class="_fw3ds"><span class="_17z9g _c4mil"><!-- react-text: 7716 -->Language<!-- /react-text --><select class="_nif11"><option value="af">Afrikaans</option><option value="cs">Čeština</option><option value="da">Dansk</option><option value="de">Deutsch</option><option value="el">Ελληνικά</option><option value="en">English</option><option value="es">Español</option><option value="fi">Suomi</option><option value="fr">Français</option><option value="id">Bahasa Indonesia</option><option value="it">Italiano</option><option value="ja">日本語</option><option value="ko">한국어</option><option value="ms">Bahasa Melayu</option><option value="nb">Norsk</option><option value="nl">Nederlands</option><option value="pl">Polski</option><option value="pt-br">Português (Brasil)</option><option value="pt">Português (Portugal)</option><option value="ru">Русский</option><option value="sv">Svenska</option><option value="th">ภาษาไทย</option><option value="tl">Filipino</option><option value="tr">Türkçe</option><option value="zh-cn">中文(简体)</option><option value="zh-tw">中文(台灣)</option><option value="bn">বাংলা</option><option value="gu">ગુજરાતી</option><option value="hi">हिन्दी</option><option value="hr">Hrvatski</option><option value="hu">Magyar</option><option value="kn">ಕನ್ನಡ</option><option value="ml">മലയാളം</option><option value="mr">मराठी</option><option value="ne">नेपाली</option><option value="pa">ਪੰਜਾਬੀ</option><option value="si">සිංහල</option><option value="sk">Slovenčina</option><option value="ta">தமிழ்</option><option value="te">తెలుగు</option><option value="ur">اردو</option><option value="vi">Tiếng Việt</option><option value="zh-hk">中文(香港)</option><option value="bg">Български</option><option value="fr-ca">Français (Canada)</option><option value="ro">Română</option><option value="sr">Српски</option><option value="uk">Українська</option></select></span></li></ul></nav><span class="_es4h6">© 2017 Instagram</span></div></footer><!-- react-empty: 7767 --><div class="_s1xpw _44kmz"><div class="_plwoo"></div></div></section>

1 个答案:

答案 0 :(得分:1)

如果您无法修改HTML,则无法在此处使用following-sibling机制。事实是,每个<a>链接都没有任何兄弟姐妹 - 他们是<div>个父母的单身孩子。我还注意到每个父级也包含在前一个父级<div>中,从而产生一组嵌套的链接。

我认为您最好的选择是使用WebElement.findElements()方法来查找<a>元素列表并循环处理结果。 注意:我还在xpath的开头添加了'.'字符,仅搜索mainelem结果的上下文中。

WebElement mainelem = url.findElement(By.tagName("main"));
List<WebElement> piclinks = mainelem.findElements(By.xpath(".//a[starts-with(@href, '/p/')]"));

for (WebElement picLink : piclinks) {
    //... picLink processing...
}