获取网页某个元素座标的方法

用python的selenium写爬虫通过绝对座标点击,座标的获取方法(谷歌浏览器):

网页F12打开源代码,在console窗口输入

document.querySelectorAll('css选择器')[0].getBoundingClientRect().left + document.documentElement.scrollLeft
document.querySelectorAll('
css选择器')[0].getBoundingClientRect().top + document.documentElement.scrollTop

其中css选择器为目标区域的css表达式,Left计算X座标,Top计算Y座标,若该css区域过大,需计算其区域大小:

document.querySelectorAll('css选择器')[0].getBoundingClientRect().width
document.querySelectorAll('
css选择器')[0].getBoundingClientRect().height 

width计算区域宽度,height计算区域高度,座标原点为网页视图左上角顶点,可通过计算后精准点击某点

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章