我有一个在这里以某种方式讨论过的问题([python][selenium] on-screen position of element),但是目前还没有达到我想要达到的目的。
我的目标是: element.location给出元素在浏览器中的左上角的位置。我有一个网站,即使它可能不是很好的硒实践,我也希望能够纯粹根据其位置单击该元素,因为它从未改变过,而且可能永远也不会改变。 假设element.location给出{'x':253,'y':584},这是我到目前为止尝试的没有运气的代码
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()
当我运行此代码时,什么都没有发生。除了打开一个新窗口外,我只会。 有人可以帮忙吗?
答案 0 :(得分:1)
最好在元素的中间单击,而不是在其角落单击。有时无法点击角点。 “ openwindow”元素的坐标x和y是其左上角的坐标。
我建议计算元素中心的坐标。为此,请首先检查元素的宽度和高度:
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()
答案 1 :(得分:0)
尝试一下:
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().perform()
y_coordinate = open_window_elem.location["y"]
x_coordinate = open_window_elem.location["x"]
action = ActionChains(driver)
action.move_by_offset(int(x_coordinate), int(y_coordinate))
action.click().perform()
答案 2 :(得分:0)
这是一个解决方案,基于(Clicking at coordinates without identifying element)上的最后一个答案,由于我的原始代码中发布的网站不起作用,我不得不对其进行调整:
# WORKING EXAMPLE 3
# assumptions is I know what coordinate I want to use
# in this example we use x = 253, y = 584
# remember: y = 584 -> it will move down (a negative value moves up)
zero_elem = driver.find_element_by_tag_name('body')
x_body_offset = zero_elem.location["x"]
y_body_offset = zero_elem.location["y"]
print("Body coordinates: {}, {}".format(x_body_offset, y_body_offset))
x = 253
y = 310
actions = ActionChains(driver)
actions.move_to_element_with_offset(driver.find_element_by_tag_name('body'), -x_body_offset, -y_body_offset).click()
actions.move_by_offset( x, y ).click().perform()
基本上,身体坐标不一定是0,0,这就是为什么我不得不使用x_body_offset和y_body_offset。