python - 在Python中获取两个网络元素之间的距离?

标签 python web browser distance element

说我有

<body>
    <div id="container1"><div>
    <div id="container2"><div>
</body>

有没有办法在浏览器中渲染后获取 container1container2 之间的距离?我所说的距离是指用户在两个元素之间看到的实际距离,以像素、英寸或其他形式表示。

最佳答案

这非常依赖于平台/浏览器,但您可以尝试 Selenium + PhantomJS。例如,让我们找到 main Wikipedia page 中两个链接之间的距离。 : enter image description here

安装依赖项:

  1. 安装 NodeJS:
  2. 安装 phantomjs:npm -g install phantomjs
  3. 安装selenium:pip install selenium

Python:

from selenium import webdriver

driver = webdriver.PhantomJS()
driver.set_window_size(1024, 768)
driver.get('http://en.wikipedia.org/wiki/Main_Page')

main_page_link = driver.find_element_by_css_selector('#n-mainpage-description a')
help_link = driver.find_element_by_css_selector('#n-help a')

help_link.location['y'] - main_page_link.location['y']
# 159 px

对于 Chrome/Firefox 等,您只需安装相应的网络驱动程序并更改:

driver = webdriver.Chrome()  # or
driver = webdriver.Firefox()

作为渲染差异的示例,在 Mac 中使用 Chrome 时我得到 171 像素,而在 Firefox 中我得到 164 像素。

关于python - 在Python中获取两个网络元素之间的距离?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25821284/

相关文章:

java - 用于 Python 的 JFileChooser?

javascript - 使用 html 代码和 javascript 代码作为 flutter web 中的小部件

ios - Facebook 和 Twitter 移动应用程序是否使用 Safari 浏览器?

java - OS 系统更新后 Eclipse 内部 Web 浏览器呈现不正确

python - 根据每行中的值获取列标题

python - 使用seaborn绘制pandas数据框系列

python - 嵌套构造函数。为什么需要它?

python - 未找到 AWS ec2 winreg

html - 尝试向导航栏添加新元素并向网站添加新页面[找不到错误]

php - 如何在html中嵌入pdf文件?