【问题标题】:Selenium .text doesn't get text that i wantSelenium .text 没有得到我想要的文本
【发布时间】:2022-06-14 09:08:31
【问题描述】:

这里是 HTML 代码:

<h4 class="page-header">
  " (111) Senior "
  <small>
    <font class="small"> 
      <i class="glyphicon glyphicon-road">
        ::before
      </i>
      " Street 8.9 "
      <a href="htttp://" target="_blank">(master refrence)</a>
    </font>
  </small>
</h4>

我的 Python 代码:

print("new adress: " + driver.find_element(By.CLASS_NAME, "small").text)

我的结果:

new address: (Verification data, according to school updates)

我想要这样的结果:

new address:  Street 8.9

有人对我的问题提出建议吗?

【问题讨论】:

    标签: python-3.x selenium selenium-webdriver


    【解决方案1】:

    你最好使用find_element_by_css_selector函数。

    print("new adress: " + driver.find_element_by_css_selector("h4.page-header font.small").text)
    

    【讨论】:

    • 我仍然无法理解如何填充值函数find_element_by_css_selector,因为我使用了你的建议,但我得到了异常selenium.common.exceptions.NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":"h4.page-header font.small"}
    • print("alamat terbaru: "+ driver.find_element(By.CSS_SELECTOR,"font.small").text) 我使用它,但我仍然得到相同的结果new address: (Verification data, according to school updates)
    • 首先需要在浏览器控制台中搜索h4.page-header font.small
    • 你找到标签了吗?
    • 我已经找到了我的代码的主要问题。可能会发生错误,因为我忘记将链接移动到新的一个原因,因为我无法获得我想要的价值。这就是为什么我得到selenium.common.exceptions.NoSuchElementException:
    猜你喜欢
    • 2020-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多