使用Python在Selenium WebDriver中获取WebElement的HTML源 [英] Get HTML Source of WebElement in Selenium WebDriver using Python

查看:415
本文介绍了使用Python在Selenium WebDriver中获取WebElement的HTML源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Python绑定来运行Selenium WebDriver:

I'm using the Python bindings to run Selenium WebDriver:

from selenium import webdriver
wd = webdriver.Firefox()

我知道我可以像这样抓取网络元素:

I know I can grab a webelement like so:

elem = wd.find_element_by_css_selector('#my-id')

我知道我可以通过...获得整页的源代码.

And I know I can get the full page source with...

wd.page_source

但是反正有获取元素来源"的信息吗?

But is there anyway to get the "element source"?

elem.source   # <-- returns the HTML as a string

Python的Selenium Webdriver文档基本上不存在,我在代码中看不到任何能够启用该功能的东西.

The selenium webdriver docs for Python are basically non-existent and I don't see anything in the code that seems to enable that functionality.

是否对访问元素(及其子元素)的HTML的最佳方法有何想法?

Any thoughts on the best way to access the HTML of an element (and its children)?

推荐答案

您可以阅读innerHTML属性以获取元素的 content 的来源,或者通过outerHTML获取当前元素的来源

You can read innerHTML attribute to get source of the content of the element or outerHTML for source with the current element.

Python:

element.get_attribute('innerHTML')

Java:

elem.getAttribute("innerHTML");

C#:

element.GetAttribute("innerHTML");

Ruby:

element.attribute("innerHTML")

JS:

element.getAttribute('innerHTML');

PHP:

$element->getAttribute('innerHTML');

经过测试,可与ChromeDriver一起使用.

Tested and works with the ChromeDriver.

这篇关于使用Python在Selenium WebDriver中获取WebElement的HTML源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆