我想找到" 160528"下面的html代码中的元素:
<td class="calCell">
<div id="cal1">
<div align="center">
<table id="table1">
<tbody>
<tr></tr>
<td>
<a id="160528"></a>
</td>
</tbody>
</table>
</div>
</div>
</td>
我做的是:
calCell = driver.find_element_by_class_name('calCell')
cal1 = calCell.find_element_by_id('cal1')
table1 = cal1.find_element_by_id('table1')
该程序会抱怨&#34;无法找到元素&#34;,然而table1确实存在。
有没有人可以分享如何找到160528的灯?除了逐个转到子元素之外,还有更简单的方法吗?
答案 0 :(得分:1)
搜索时,该表可能不存在。添加等待:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.select import Select
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
wait = WebDriverWait(driver, 10)
table1 = wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, ".calCell #cal1 #table1")))