我有一个如下所示的网页:
<table class="data" width="100%" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<th>1</th>
<th>2</th>
<th>3 by</th>
</tr>
<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>
<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>
</tr>
</tbody>
</table>
我想将这两个网站放在某个地方(interested_site1 和 interested_site2)。我试着做这样的事情:
chrome = webdriver.Chrome(chrome_path)
chrome.get("fooSite")
time.sleep(.5)
alert = chrome.find_element_by_xpath("/div/table/tbody/tr[2]/td[2]").text
print (alert)
但是我找不到第一个站点。如果我不能在 for 循环中执行此操作,我不介意分别获取每个链接。我怎样才能到达那个链接?
最佳答案
使用 CSS 查询会更容易:
driver.find_element_by_css_selector("td:nth-child(2)")
关于python - 使用 Selenium 在 Python 中获取所有表元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45347431/