1

我是 selenium 的新手,一直(PYTHON)卡在某一点上,需要专家的帮助。我的html是这样的:

    <div id='d3_tree'>
       <svg>
         <g transform="translate(20,50)>
            <g class='node'>
                <foreignobject></foreignobject>
                <original_title>
                   <table>
                       <tbody>
                          <tr>
                              <td>t1key1</td>
                              <td>t1val1</td>  
                          </tr> 
                          <tr>
                              <td>t1key2</td>
                              <td>t1val2</td>  
                          </tr> 
                          <tr>
                              <td>t1key3</td>
                              <td>t1val3</td>  
                          </tr> 
                       </tbody> 
                   </table>
                </original_title> 
            </g> 
            <g class='node pe_node'>
                <foreignobject></foreignobject>
                <original_title>
                   <table>
                       <tbody>
                          <tr>
                              <td>t2key1</td>
                              <td>t2val1</td>  
                          </tr> 
                          <tr>
                              <td>t2key2</td>
                              <td>t2val2</td>  
                          </tr> 
                          <tr>
                              <td>t2key3</td>
                              <td>t2val3</td>  
                          </tr> 
                       </tbody> 
                   </table>
                </original_title> 
            </g>
            <g class='node pe_node'>
                <foreignobject></foreignobject>
                <original_title>
                   <table>
                       <tbody>
                          <tr>
                              <td>t3key1</td>
                              <td>t3val1</td>  
                          </tr> 
                          <tr>
                              <td>t3key2</td>
                              <td>t3val2</td>  
                          </tr> 
                          <tr>
                              <td>t3key3</td>
                              <td>t3val3</td>  
                          </tr> 
                       </tbody> 
                   </table>
                </original_title> 
            </g> 
         </g>
       </svg>

</div>

我需要的是具有类 node.pe_node 的所有元素,并且在每个 node.pe 节点元素内我需要表格第三行第二列的文本。(t2val3 t3val3)

我能够获得具有类 node.pe_node 的元素

pe_nodes = self.driver.find_elements_by_css_selector(".node.vm.node_pe")

现在我正在遍历 pe_nodes 以获取第三列的值

  for node in pe_nodes:
     petext = node.find_element(By.XPATH, "//tr[3]/td[2]").text //not working
     petext = node.find_element(By.XPATH, "//tr[3]/td[2]").get_text() //not working

谁能指导我如何获得所需的文本?有没有办法引用每个节点元素内的表列?

4

2 回答 2

1
import bs4

soup = bs4.BeautifulSoup(html)
node_pe = [s for s in soup.find_all('g')
           if 'pe_node' in s.attrs.get('class', [])]
col_texts = [s.find_all('tr')[2].find_all('td')[1].text
             for s in node_pe]
print col_texts

它产生:

[u't2val3', u't3val3']
于 2013-05-17T06:51:48.577 回答
1

我找到了路

name = node.find_element_by_xpath(".//tr[2]/td[2]")text
于 2013-05-17T06:52:56.297 回答