我正在尝试打印表格单元格的文本,但是没有打印任何内容。它在标识元素,但似乎无法找到文本。
HTML看起来像这样:
<tr style="font-weight: 600;">
<td>
"
Available Balance
"
</td>
<td id="testBalance" class="text-right ng-binding" style="font-weight: 600;">
664,265.314
</td>
我正在尝试通过运行读取(并打印)“ 664,256.314”的值:
Balance=driver.find_element_by_id("testBalance")
print (Balance.text)
它找到了元素,但是什么也没打印。有人知道为什么会这样吗?
最佳答案
我认为这只是一个时间问题-在您获取余额时,尚未设置/更新余额。添加Explicit Wait以等待文本出现:
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class wait_for_any_text(object):
def __init__(self, locator, text_):
self.locator = locator
self.text = text_
def __call__(self, driver):
try:
element_text = EC._find_element(driver, self.locator).text
return element_text.strip()
except StaleElementReferenceException:
return False
wait = WebDriverWait(driver, 10)
balance = wait.until(wait_for_any_text((By.ID, "testBalance")))
print(balance.text)