我想编写一个函数get_text
来从表td标签中获取文本。但是文本可以在tds中以不同的方式包装/设置样式。到目前为止,我有以下代码。我无法弄清楚如何在案例1和案例2中列出两个文本
def get_text(xpath):
e = self.driver.find_elements_by_xpath('{}//*[not(./*)]'.format(xpath))
if not e:
e = self.driver.find_element_by_xpath(xpath)
return [e.text]
return [o.text for o in e]
case 1
<td class = "" >
<span >
<span >
<span > prefix text 1 < /span >
< / span >
text 1
< / span >
< / td >
# return ['prefix text 1', 'text 1']
case 2:
<td class = "" >
<div >
<span > prefix text 2 < /span >
< / div >
text 2
< / td >
# return ['prefix text 2', 'text 2']
case 3:
<td class = "" >
test3
< / td >
# return ['test3']
case 4:
<div >
<a href = "#" >
link text 4
< / a >
<div >
<span >
<span > prefix text 4 < /span >
< / span >
<span >
text 4
< / span >
< / div >
< / div >
# return ['link text 4', 'prefix text 4', 'text 4']