【问题标题】:Python Selenium accessing HTML sourcePython Selenium 访问 HTML 源代码
【发布时间】:2011-12-13 06:57:40
【问题描述】:

如何通过 Python 使用 Selenium 模块在变量中获取 HTML 源?

我想做这样的事情:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://example.com")
if "whatever" in html_source:
    # Do something
else:
    # Do something else

我该怎么做?我不知道如何访问 HTML 源代码。

【问题讨论】:

  • 如果条件之前写下一行:html_source = browser.page_source

标签: python selenium selenium-webdriver


【解决方案1】:

您需要访问page_source 属性:

from selenium import webdriver

browser = webdriver.Firefox()
browser.get("http://example.com")

html_source = browser.page_source
if "whatever" in html_source:
    # do something
else:
    # do something else

【讨论】:

  • 迄今为止最好的答案!执行此操作的最直接和最清晰的方法,比其他仍然有效的替代方法(find_element_by_xpath("//*").get_attribute("outerHTML")(
  • 如果我们需要在所有 javascript 执行后获取页面源代码怎么办?
  • 只有在页面完全加载后才有效。如果页面无限期加载,则此属性不起作用。
【解决方案2】:
from bs4 import BeautifulSoup
from selenium import webdriver

driver = webdriver.Chrome()
html_source_code = driver.execute_script("return document.body.innerHTML;")
html_soup: BeautifulSoup = BeautifulSoup(html_source_code, 'html.parser')

现在您可以应用 BeautifulSoup 函数来提取数据...

【讨论】:

    【解决方案3】:

    driver.page_source 将帮助您获取页面源代码。您可以检查页面源中是否存在文本。

    from selenium import webdriver
    driver = webdriver.Firefox()
    driver.get("some url")
    if "your text here" in driver.page_source:
        print('Found it!')
    else:
        print('Did not find it.')
    

    如果要将页面源存储在变量中,请在 driver.get 之后添加以下行:

    var_pgsource=driver.page_source
    

    并将 if 条件更改为:

    if "your text here" in var_pgsource:
    

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关它如何和/或为什么解决问题的额外上下文将提高​​答案的长期价值。
    【解决方案4】:

    使用 Selenium2Library,您可以使用 get_source()

    import Selenium2Library
    s = Selenium2Library.Selenium2Library()
    s.open_browser("localhost:7080", "firefox")
    source = s.get_source()
    

    【讨论】:

    • 我可以设置延迟并获取最新源吗?有使用 javascript 加载的动态内容。
    【解决方案5】:

    通过使用页面源,您将获得整个 HTML 代码。
    因此,首先确定您需要在其中检索数据或单击元素的代码块或标记..

    options = driver.find_elements_by_name_("XXX")
    for option in options:
        if option.text == "XXXXXX":
            print(option.text)
            option.click()
    

    您可以按名称、XPath、id、链接和 CSS 路径查找元素。

    【讨论】:

      【解决方案6】:

      要回答有关让 URL 用于 urllib 的问题,只需执行以下 JavaScript 代码:

      url = browser.execute_script("return window.location;")
      

      【讨论】:

        【解决方案7】:

        您可以简单地使用WebDriver 对象,并通过其@property 字段page_source 访问页面源代码...

        试试这个代码 sn-p :-)

        from selenium import webdriver
        driver = webdriver.Firefox('path/to/executable')
        driver.get('https://some-domain.com')
        source = driver.page_source
        if 'stuff' in source:
            print('found...')
        else:
            print('not in source...')
        

        【讨论】:

        【解决方案8】:

        我建议使用urllib 获取源代码,如果要解析,请使用Beautiful Soup 之类的内容。

        import urllib
        
        url = urllib.urlopen("http://example.com") # Open the URL.
        content = url.readlines() # Read the source and save it to a variable.
        

        【讨论】:

        • 好的,那么你知道我如何在 Selenium 中获取 URL 吗?我想将 URL 存储在一个变量中,以便我可以使用 urllib 访问它。
        • @user1008791 这有关系吗?您显然是让用户使用 raw_input 输入它,只是使用 urllib 做同样的事情。
        • 这只是为了做一个简单的例子,网址会发生很大变化。
        • Selenium 做了很多 urllib 没有做的事情(例如 JavaScript 的执行)。
        • 这里使用urllib是没有意义的,为什么呢? AutomatedTester 是正确的,这是我扫描 HTML 源代码以确保我们不会推送开发环境代码。
        猜你喜欢
        • 2016-05-12
        • 2020-05-13
        • 1970-01-01
        • 2023-03-07
        • 1970-01-01
        • 2015-06-30
        • 1970-01-01
        • 1970-01-01
        • 2014-03-06
        相关资源
        最近更新 更多