Jiun Jung Lin
Jiun Jung Lin

Reputation: 157

How to get only rely content (not included quote content) using Selenium

I want to know what to get some content not include quote content.

https://forumd.hkgolden.com/view.aspx?type=BW&message=7219211

The following picture is the example

enter image description here

I want to get only "唔提冇咩人記得", but I use the following code will get both content.

content = driver_blank.find_element_by_xpath('/html/body/form/div[5]/div/div/div[2]/div[1]/div[5]/table[24]/tbody/tr/td/table/tbody/tr/td[2]/table/tbody/tr[1]/td/div')
print(content.text)

The following code is what I want to capture content:

<div class="ContentGrid">
<blockquote><div style="color: #0000A0;"><blockquote><div style="color: #0000A0;">腦魔都俾你地bam咗啦<img data-icons=":~(" src="/faces/cry.gif" alt=":~("></div></blockquote><br>珠。。。。。</div></blockquote><br>唔提冇咩人記得
<br><br><br>
</div>

enter image description here

Can anyone help me? Thanks~~~

Can not(starts-with 's method be solved?

Upvotes: 0

Views: 142

Answers (1)

NarendraR
NarendraR

Reputation: 7708

Use below line of code to extract only text node content

element = driver.find_element_by_css_selector('div.ContentGrid')
text = driver.execute_script("return arguments[0].childNodes[3].textContent", element);
print(text)

Selenium won't allow you to directly locate an element using text node. Though you can use some JavaScript code to make it happen.

Code Explanation:

arguments[0].childNodes[3] indicates 3rd child element of your context node which is div.ContentGrid. Please note first 2 child element of the context node are blank (tried with the HTML code shared by you) that's why index 3 used.

Upvotes: 1

Related Questions