【问题标题】:Unable to extract attribute value with selenium无法用硒提取属性值
【发布时间】:2021-03-18 08:52:56
【问题描述】:
caller = driver.find_element_by_id("sys_display.new_call.caller") 
print(caller.get_attribute('value'))

您好,在尝试从元素中提取属性值时遇到问题。 我试过使用caller= driver.find_element_by_id("sys_display.new_call.caller").get_attribute('value') 但它似乎根本没有从 HTML 中提取价值。

我是菜鸟,谢谢你的帮助!

【问题讨论】:

  • 您能否添加您尝试提取的值的屏幕截图
  • 请添加您要提取数据的 URL?
  • 我要提取的值是 MyMercy User

标签: python-3.x selenium xpath css-selectors webdriverwait


【解决方案1】:

在 selenium 中,如果值存储为 innerText,则必须使用 .text 方法检索它,这仅在元素在 UI 中可见时才有效。表示如果它的隐藏或不在视图端口 .text 方法将返回空白。在这种情况下,您可以使用 get attribute "textContent" 。但是不推荐测试 textCONtent,因为它不会验证文本是否显示在 UI 中。建议仅用于网络抓取

你也可以在打印之前等待 value 字段有文本你确实在寻找 value 属性

from selenium.webdriver.support import expected_conditions as EC

wait = WebDriverWait(driver, 10)
caller = wait.until(EC.text_to_be_present_in_element_value((By.ID, 'sys_display.new_call.caller'),"MyMercy User"))

caller = driver.find_element_by_id("sys_display.new_call.caller") 
print(caller.get_attribute('value'))
print(caller.text)
print(caller.get_attribute('textContent'))

尝试打印这三个值,看看哪一个给出了你想要的结果

【讨论】:

    【解决方案2】:

    要打印 value 属性的值,即MyMercy User,您可以使用以下任一Locator Strategies

    • 使用css_selector

      print(driver.find_element_by_css_selector("input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']").get_attribute("value"))
      
    • 使用xpath

      print(driver.find_element_by_xpath("//input[@id='sys_display.new_call.caller' and @data-name='caller']").get_attribute("value"))
      

    理想情况下,您需要为visibility_of_element_located() 诱导WebDriverWait,您可以使用以下任一Locator Strategies

    • 使用CSS_SELECTOR

      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[id^='sys_display'][id*='new_call'][id$='caller'][data-name='caller']"))).get_attribute("value"))
      
    • 使用XPATH:

      driver.get('https://www.temporary-mail.net/')
      print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//input[@id='sys_display.new_call.caller' and @data-name='caller']"))).get_attribute("value"))
      
    • 控制台输出:

      MyMercy User
      
    • 注意:您必须添加以下导入:

      from selenium.webdriver.support.ui import WebDriverWait
      from selenium.webdriver.common.by import By
      from selenium.webdriver.support import expected_conditions as EC
      

    【讨论】:

    • @PDHide 让我们在Selenium Chat Room中详细讨论这个问题
    • 所以这些一直给我一个错误: Traceback(最近一次调用最后一次):文件“c:/Users/jjudi/OneDrive/Documents/Python Scripts/import selenium.py”,第 40 行,在 打印(WebDriverWait(驱动程序,20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "input[id^='sys_display'][id*='new_call'][id$='caller'] [data-name='caller']"))).get_attribute("value")) 文件“C:\Users\jjudi\anaconda3\lib\site-packages\selenium\webdriver\support\wait.py”,行80,在直到 raise TimeoutException(message, screen, stacktrace) selenium.common.exceptions.TimeoutException: Message:
    • @Justin.U 非常期待,因为您没有提供基于文本的 HTML,这将有助于我们在发布之前测试我们自己的解决方案。
    • 对不起,它不是一个公共网站,所以我不想发布太多关于它的信息。我非常感谢您的帮助,我是否需要指定它的主要功能?
    猜你喜欢
    • 1970-01-01
    • 2017-09-26
    • 2021-10-28
    • 2015-03-29
    • 1970-01-01
    • 2016-10-16
    • 2021-04-15
    • 1970-01-01
    • 2011-02-06
    相关资源
    最近更新 更多