【问题标题】:Get html of inspect element source with selenium使用 selenium 获取检查元素源的 html
【发布时间】:2019-10-04 00:43:34
【问题描述】:

我正在使用 Chrome 使用 selenium。 我正在访问的网页动态更新。 我需要显示结果的 html,我可以在“检查元素”时访问它。 我不知道如何从我的代码中访问该 html。我总是得到原始的 html。

我试过这个:Get HTML Source of WebElement in Selenium WebDriver using Python

browser.get('http://bijsluiters.fagg-afmps.be/?localeValue=nl')
searchform = browser.find_element_by_class_name('iceInpTxt')
searchform.send_keys('cefuroxim')
button = browser.find_element_by_class_name('iceCmdBtn').click()

element = browser.find_element_by_class_name('contentContainer')
html = element.get_attribute('innerHTML')
browser.close()
print(html)

【问题讨论】:

    标签: python selenium beautifulsoup


    【解决方案1】:

    它似乎在延迟一段时间后开始工作。如果我是你,我应该尝试尝试延迟时间。

    from selenium import webdriver
    import time
    
    browser = webdriver.Chrome()
    
    browser.get('http://bijsluiters.fagg-afmps.be/?localeValue=nl')
    searchform = browser.find_element_by_class_name('iceInpTxt')
    searchform.send_keys('cefuroxim')
    button = browser.find_element_by_class_name('iceCmdBtn').click()
    
    time.sleep(10)
    
    element = browser.find_element_by_class_name('contentContainer')
    html = element.get_attribute('innerHTML')
    browser.close()
    print(html)
    

    添加:更好的方法是在元素可用时让脚本继续执行(因为在将特定元素添加到 DOM 之前(例如)使用 JS 需要时间)。在您的示例中要查找的元素是 ID 为 iceDatTbl 的表格(快速查看后我可以找到)。

    【讨论】:

    • 你是对的,它有效。现在我觉得很愚蠢,我之前尝试过,但延迟只有 1 秒,因为我在手动操作时看到了即时结果。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多