1

I am new to selenium (PYTHON) and stuck at one point and need help from experts. My html is some thing like this:

    <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>

what I need is all the elements of having class node.pe_node and inside every node.pe node element I need the text of third row second column of table. (t2val3 t3val3)

I am able to get the elements having class node.pe_node

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

Now I am iterating over the pe_nodes to get the the value of third column by

  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

Can any one please guide me as how to get the required text? Is there a way refer the table column inside every node element?

2
  • You miss a quote mark in the third line, after translate Commented May 17, 2013 at 6:52
  • I would use @JakubM. example, beatiful soup is very easy to use when parsing html Commented May 17, 2013 at 6:56

2 Answers 2

1

I have found the way

name = node.find_element_by_xpath(".//tr[2]/td[2]")text
Sign up to request clarification or add additional context in comments.

1 Comment

Dot text missing?
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

It produces:

[u't2val3', u't3val3']

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.