iman_sh77
iman_sh77

Reputation: 77

separate texts from a href in same td with XPath python

I have an HTML webpage like this:

<tr><td style="text-align:center;">7</td><td class="multi_row" style="line-height:15px;">Loaded on 'NYK LEO 303W' at Port of Loading<br> <a href="JavaScript:void(0);" style="line-height:15px;" title="NYK LEO" data-click="vesselPop" data-cd="NLZT0303W">NYK LEO 303W</a></td><td class="multi_row" style="line-height:15px;">VANCOUVER, BC ,CANADA<br>  <a href="JavaScript:void(0);" style="line-height:15px;" onclick="openLocationPopup('CAVAN01')" title="3891 DELTAPORT GCT">3891 DELTAPORT GCT</a></td><td class="ico_e">2018-10-26 23:30</td></tr>

I want to separate the <a href>'s string part in one variable and have a pure text like 'bla bla bla' in another variable. this is what i have done till now:

event_path = driver.find_elements_by_xpath("//table[@id='detail']//tr/td[2]")
event = [cell.text for cell in event_path]

its for the text part and this one is for the string in :

vessel_path = driver.find_elements_by_xpath("//table[@id='detail']//tr/td[2]/a")
vessel = [cell.text.split(' ')[:2] for cell in vessel_path]

the split(' ')[:2] is cuz the data is sth like this : NYK LEO 303W and i just need words not the number (it can be done more reliable with regex)

Upvotes: 1

Views: 212

Answers (3)

Navarasu
Navarasu

Reputation: 8489

In your case, I see vessel that you are expecting is already present in title attribute of anchor.

If it is a valid case, then you can get it directly from attibutes like,

vessel_path = driver.find_elements_by_xpath("//table[@id='detail']//tr/td[2]/a")
vessel = [cell.get_attribute("title") for cell in vessel_path]

Upvotes: 0

Andersson
Andersson

Reputation: 52685

Try to use below to get only first text node from td

event = [driver.execute_script('return arguments[0].firstChild.textContent;', cell).strip() for cell in event_path]

Upvotes: 2

sayhan
sayhan

Reputation: 1184

Please try following code :

elements = driver.find_elements_by_classname("multi_row")

for element in elements
    print(element.text)

Upvotes: 0

Related Questions