Python Selenium - 纯粹根据基于身体元素偏移的位置点击元素 [英] Python Selenium - click on element purely based on its location based on body element offset

查看:102
本文介绍了Python Selenium - 纯粹根据基于身体元素偏移的位置点击元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题在这里讨论过([python][selenium] on- 元素的屏幕位置),但目前无法实现我想要实现的目标.

I have a question that was somehow discussed here ([python][selenium] on-screen position of element) but that doesn't currently do what I'm trying to achieve.

我的目标如下:element.location 给出元素在浏览器中左上角的位置.我有一个网站,即使它可能不是一个很好的 selenium 实践,我也希望能够纯粹根据它的位置点击这样的元素,因为它从未改变过,而且可能永远不会改变.假设 element.location 给出 {'x': 253, 'y': 584},这是我到目前为止尝试的代码,但没有运气

My goal is the following: element.location gives the position of the top left corner of element in the browswer. I have a website in which, even if it's probably not a good selenium practice, I want to be able to click on such element purely based on its position because it has never changed and likely never will. Assuming element.location gives {'x': 253, 'y': 584}, this is the code I tried so far with no luck

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver.maximize_window()
url = "https://learn.letskodeit.com/p/practice"
driver.get(url)
open_window_elem = driver.find_element_by_id("openwindow")

# from wherever the mouse is, I move to the top left corner of the broswer
action = ActionChains(driver)
action.move_by_offset(-1000, -1000)    
action.click()
action.perform()

y_coordinate = open_window_elem.location["y"]
x_coordinate = open_window_elem.location["x"]

action = ActionChains(driver)
action.move_by_offset(x_coordinate, y_coordinate)
action.click()
action.perform()

当我运行此代码时没有任何反应.我宁愿打开一个新窗口.有人可以帮忙吗?

Nothing happens when I run this code. I would except to open a new window. Can somebody help?

推荐答案

点击元素的中间比点击它的角落要好.有时角是不可点击的.openwindow"元素的坐标 x 和 y 这些是其左上角的坐标.

It is better to click in the middle of the element than in its corner. Sometimes corners are not clickable. Coordinates x and y of "openwindow" element these are the coordinates of its upper left corner.

我建议计算元素中心的坐标.为此,首先检查元素的宽度和高度:

I suggest calculating the coordinates of the element's center. To do this, first check the width and height of the element:

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
driver.maximize_window()
url = "https://learn.letskodeit.com/p/practice"
driver.get(url)

open_window_elem = "//button[@id='openwindow']"

x = int(driver.find_element_by_xpath(open_window_elem).location['x'])
y = int(driver.find_element_by_xpath(open_window_elem).location['y'])
width = int(driver.find_element_by_xpath(open_window_elem).size['width'])
height = int(driver.find_element_by_xpath(open_window_elem).size['height'])

action = webdriver.common.action_chains.ActionChains(driver)
action.move_by_offset(x + width/2, y + height/2)
action.click()
action.perform()

这篇关于Python Selenium - 纯粹根据基于身体元素偏移的位置点击元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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