python - 使用 Selenium Python 从表 (td) 读取文本后不打印任何内容

标签 python html selenium

我正在尝试打印表格单元格的文本,但没有打印任何内容。它正在识别元素,但似乎无法找到文本。

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)

关于python - 使用 Selenium Python 从表 (td) 读取文本后不打印任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37993099/

相关文章:

python - django:为可以是作家或 Actor 或两者的人创建模型

java - Selenium/Java/Junit - 获取运行测试时使用的驱动程序/浏览器

ruby - Cucumber、capybara 和 selenium - 提交没有按钮的表单

python - 如何增加 imaplib 请求的超时时间?

python - 使用 Sphinx 创建 PDF 时如何避免 "too deeply nested"错误?

python - while 不是表达式和​​ boolean 理解

javascript - Bootstrap 下拉菜单不会出现 onclick

html - R 中 htmlwidget 的 savewidget,无法将 html 文件保存在另一个文件夹中

javascript - 我似乎无法找到一种方法来获取 swisnl 的 jQuery ContextMenu 上的单选按钮组单击事件

java - Selenium WebDriver 中 org.openqa.selenium.remote.session.StripAnyPlatform 类的用途是什么?