Python Selenium - 单击某个位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/16807258/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Selenium - Click at certain position
提问by davids
Using the Python version of Selenium, is it possible to click some element in the DOM and to specify the coordinates where you want to click it?
The Java version has the method clickAt, which actually does exactly what I am looking for, but can't find the equivalent in Python.
使用 Python 版本的 Selenium,是否可以单击 DOM 中的某个元素并指定要单击它的坐标?Java 版本有 method clickAt,它实际上完全符合我的要求,但在 Python 中找不到等价物。
回答by Ewan
I've not personally used this method, but looking through the source code of selenium.pyI've found the following methods that look like they'd do what you want - They look to wrap clickAt:
我没有亲自使用过这种方法,但是通过查看selenium.py我发现的以下方法的源代码,它们看起来像您想要的那样-它们看起来要包装clickAt:
def click_at(self,locator,coordString):
    """
    Clicks on a link, button, checkbox or radio button. If the click action
    causes a new page to load (like a link usually does), call
    waitForPageToLoad.
    'locator' is an element locator
    'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse      event relative to the element returned by the locator.
    """
    self.do_command("clickAt", [locator,coordString,])
def double_click_at(self,locator,coordString):
    """
    Doubleclicks on a link, button, checkbox or radio button. If the action
    causes a new page to load (like a link usually does), call
    waitForPageToLoad.
    'locator' is an element locator
    'coordString' is specifies the x,y position (i.e. - 10,20) of the mouse      event relative to the element returned by the locator.
    """
    self.do_command("doubleClickAt", [locator,coordString,])
They appear in the selenium object and here is their online API documentation.
它们出现在 selenium 对象中,这里是它们的在线 API 文档。
回答by Arran
The reason you are getting confused is clickAtis an old v1 (Selenium RC) method. 
您感到困惑的原因clickAt是旧的 v1(Selenium RC)方法。
WebDriver has a slightly different concept, of 'Actions'.
WebDriver 有一个稍微不同的概念,即'Actions'。
Specifically, the 'Actions' builder for the Python bindings live here.
具体来说,Python 绑定的“操作”构建器位于此处。
The idea of the clickAtcommand is to click at a certain position relativeto a particular element.
该clickAt命令的想法是在相对于特定元素的某个位置单击。
The same is achievable within the WebDriver, using the 'Actions' builder.
同样可以在 WebDriver 中使用“操作”构建器实现。
Hopefully this updated documentationcan help.
希望这个更新的文档可以提供帮助。
回答by Pithikos
This should do it! Namely you need to use action chains from webdriver. Once you have an instance of that, you simply register a bunch of actions and then call perform()to perform them.
这个应该可以!也就是说,您需要使用来自 webdriver 的动作链。一旦你有了一个实例,你只需注册一堆动作,然后调用perform()来执行它们。
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.google.com")
el=driver.find_elements_by_xpath("//button[contains(string(), 'Lucky')]")[0]
action = webdriver.common.action_chains.ActionChains(driver)
action.move_to_element_with_offset(el, 5, 5)
action.click()
action.perform()
This will move the mouse 5 pixels down and 5 pixels right from the upper-left corner of the button I feel lucky. Then it will click().
这将使鼠标从按钮的左上角向下移动 5 个像素和向右移动 5 个像素,我感到很幸运。然后它会click()。
Notice that you mustuse perform(). Else nothing will happen.
请注意,您必须使用perform(). 否则什么都不会发生。
回答by Pranjay Kaparuwan
You can perform the task with the Action chains in the python specially with Edge browser:
您可以专门使用 Edge 浏览器使用 Python 中的 Action 链执行任务:
from selenium.webdriver import ActionChains
actionChains = ActionChains(driver)
button_xpath  = '//xapth...' 
button = driver.find_element_by_xpath(button_xpath)
actionChains.move_to_element(button).click().perform()
But sometimes Action chain does not finds the DOM element. Hence better option to use execute scipt in following way:
但有时 Action 链找不到 DOM 元素。因此,以下列方式使用 execute scipt 的更好选择:
button_xpath  = '//xapth...' 
button = driver.find_element_by_xpath(button_xpath)
driver.execute_script("arguments[0].click();", button)

