第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

如何通過Selenium從文本節(jié)點(diǎn)中提取文本?

如何通過Selenium從文本節(jié)點(diǎn)中提取文本?

手掌心 2021-09-03 16:27:15
我確實(shí)有一個(gè) HTML 代碼,我想從中獲取文本,但幾乎文本不在任何 HTML 標(biāo)簽內(nèi)。html<div class="div-estro">    <b class="">Your</b>     <b class="">ID:</b>&nbsp;3915     <b class="">Time Limit:</b>&nbsp;00:19:56     <b class="">IP:</b>&nbsp;123.101.59.87     <b class="">Membership Period:</b>&nbsp;8 year <br>    <b class="">CountryID:</b> 78     <b class="">Country:</b> US     <b class="">State:</b> OH     <b class="">City:</b> Akron     <b class="">Status:</b> Available     <b class="">Maximum Queue:</b> 4     <b class=""><br>CountryProxy:</b> 201.250.101.84:3372     <b class="">CountryIP:</b> 59.243.44.192 </div>我想從 CountryIP 和 CountryProxy 獲取文本。期待 gettext 字符串:201.250.101.84 : 3372期待 gettext 字符串:59.243.44.192我試過 xpaths ://div[@class='div-estro']//text()[12]//div[@class='div-estro']//text()[13]當(dāng)我使用 firebug 進(jìn)行評(píng)估時(shí),上面的 xpath 看起來不錯(cuò)。但是當(dāng)嘗試使用 selenium 獲取文本時(shí),我遇到了異常。
查看完整描述

2 回答

?
慕斯王

TA貢獻(xiàn)1864條經(jīng)驗(yàn) 獲得超2個(gè)贊

根據(jù)您共享的 HTML,它是一個(gè)包含文本59.243.44.192的文本節(jié)點(diǎn),因此要提取它,您可以使用以下解決方案:


WebElement myElement = driver.findElement(By.xpath("//div[@class='div-estro']"));

String myCountryIP = ((JavascriptExecutor)driver).executeScript("return arguments[0].lastChild.textContent;", myElement).toString();

更新:


根據(jù)您的評(píng)論更新,它是一個(gè)文本節(jié)點(diǎn),其中包含文本201.250.101.84:3372以便提取它,您可以使用以下解決方案:


WebElement myElement = driver.findElement(By.xpath("//div[@class='div-estro']"));

String myCountryProxy = ((JavascriptExecutor)driver).executeScript("return arguments[0].childNodes[24].textContent;", myElement).toString();


查看完整回答
反對(duì) 回復(fù) 2021-09-03
?
慕森王

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊

您可以使用 XPath 獲取所需的文本節(jié)點(diǎn),如下所示:


String countryProxy = ((JavascriptExecutor)driver).executeScript("return document.evaluate(\"//div[@class='div-estro']/b[.='CountryProxy:']/following-sibling::text()\", document, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;").toString();

String countryIP = ((JavascriptExecutor)driver).executeScript("return document.evaluate(\"//div[@class='div-estro']/b[.='CountryIP:']/following-sibling::text()\", document, null, XPathResult.ANY_TYPE, null).iterateNext().textContent;").toString();

只需更新前面b節(jié)點(diǎn)的謂詞即可獲得所需的文本:


b[.='State:']

b[.='Membership Period:']

...


查看完整回答
反對(duì) 回復(fù) 2021-09-03
  • 2 回答
  • 0 關(guān)注
  • 411 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)