【问题标题】:Selenium Python: How to extract text within a href elementSelenium Python:如何在 href 元素中提取文本
【发布时间】:2023-02-07 17:22:13
【问题描述】:

我正在寻找一种方法来提取/打印位于 href 元素内的文本。

HTML 的快照:

代码试验:

WebDriverWait(driver, 20).until(EC.visibility_of_element_located(By.XPATH('//div[contains(@class, "style1")]')))
driver.find_element_by_xpath('//div[contains(@class, "style1")]').text

错误信息:TypeError: 'str' object is not callable

多次尝试定位此“0”均未成功。当一些打开的“票”出现在系统中时,这个数字将会改变。

这怎么能做到?

【问题讨论】:

    标签: python selenium selenium-webdriver xpath webdriverwait


    【解决方案1】:

    如果你打印这个,它应该返回值0

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located(By.XPATH('//div[contains(@class, "style1")]/a'))).text)
    

    你需要改变这个

    driver.find_element_by_xpath('//div[contains(@class, "style1")]').text
    

    到

    print(driver.find_element(By.XPATH,'//div[contains(@class, "style1")]/a').text)
    

    您需要导入此库。

    from selenium.webdriver.common.by import By
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-05
      • 2021-12-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多