cosmin p cosmin p - 1 year ago 171
Python Question

Get all table elements in python using selenium

I have a webpage which looks like this:

<table class="data" width="100%" cellpadding="0" cellspacing="0">
<th>3 by</th>
<tr> <td width="10%"><a href="foo1">5120432</a></td>
<td width="70%">INTERESTED_SITE1/</td>
<td width="20%"><a href="foo2">foo2</a></td>
<tr class="alt"> <td width="10%"><a href="foo1">5120431</a></td>
<td width="70%">INTERESTED_SITE2</td>
<td width="20%"><a href="foo2">foo2</a></td>

I want to put somewhere those 2 sites (interested_site1 and interested_site2). I tried doing something like this:

chrome = webdriver.Chrome(chrome_path)

alert = chrome.find_element_by_xpath("/div/table/tbody/tr[2]/td[2]").text
print (alert)

But I can't find the first site. If I can't do this in a for-loop, I don't mind getting every link separately. How can I get to that link? Thank you!

Answer Source

It would be easier to use css query

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download