Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium-完全基于其位置(基于主体元素偏移量)单击元素_Python_Selenium Webdriver_Position_Selenium Chromedriver - Fatal编程技术网

Python Selenium-完全基于其位置(基于主体元素偏移量)单击元素

Python Selenium-完全基于其位置(基于主体元素偏移量)单击元素,python,selenium-webdriver,position,selenium-chromedriver,Python,Selenium Webdriver,Position,Selenium Chromedriver,我有一个在这里讨论过的问题(),但这并不是我目前想要达到的目标 我的目标如下: element.location给出浏览器中元素左上角的位置。我有一个网站,即使它可能不是一个好的硒实践,我希望能够完全根据它的位置点击这样的元素,因为它从未改变过,也可能永远不会改变。 假设element.location给出了{'x':253,'y':584},这就是我到目前为止尝试的代码,但没有成功 from selenium import webdriver from selenium.webdriver.c

我有一个在这里讨论过的问题(),但这并不是我目前想要达到的目标

我的目标如下: 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()
当我运行此代码时,不会发生任何事情。我只想打开一扇新窗户。 有人能帮忙吗?

试试这个:

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()

这是一个基于()上最后一个可用答案的解决方案,我不得不对其进行调整,因为它在我以原始代码发布的网站上不起作用:

# 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,这就是为什么我必须使用XyBoyDyS偏移和YyBoyDyOffice。

< P>最好在元素的中间点,而不是在它的拐角处。有时角点不可点击。“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()

当你有其他防故障选项来访问定位器时,为什么要选择location?@Abhishek_Mishra问得好。我正在创建一个自动脚本,它将首先尝试使用普通定位器获取并单击元素,但由于元素HTML语法不断变化,我正在构建第二种方法,完全根据元素的位置单击该元素,这样当DOM发生变化时,至少我的脚本会继续工作,给我时间修复代码(基于页面对象模型)要获得坐标,您需要首先获得WebElement,如果DOM发生更改,它只会在那里失败。请看这里:元素位置的可能副本永远不会更改。我知道它的x,y坐标总是相同的。这就是为什么,无论DOM是什么,我都希望selenium单击屏幕上的x,y点hanks Anish,但它在我这方面仍然不起作用,这意味着,如果它起作用,它应该单击“打开窗口”元素,但它不起作用。我必须在click()和perform()之间设置睡眠(1)