硒 - 点击某个位置

dav*_*ids 20 python selenium

使用Selenium的Python版本,是否可以单击DOM中的某个元素并指定要单击它的坐标?Java版本有这个方法clickAt,它实际上正是我正在寻找的,但在Python中找不到等价物.

Pit*_*kos 38

这应该做到!即你需要使用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()
Run Code Online (Sandbox Code Playgroud)

这会将鼠标向下移动5个像素,从按钮的左上角向右移动5个像素我觉得很幸运.然后它会click().

请注意,您必须使用perform().否则什么都不会发生.


Arr*_*ran 6

你感到困惑的原因clickAt是旧的v1(Selenium RC)方法.

WebDriver的概念略有不同,即"动作".

具体来说,Python绑定的"Actions"构建器就在这里.

clickAt命令的想法是单击对于特定元素的特定位置.

使用"操作"构建器可以在WebDriver中实现相同的功能.

希望这个更新的文档可以提供帮助.


Ewa*_*wan 3

我个人没有使用过这个方法,但是通过查看源代码,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,])
Run Code Online (Sandbox Code Playgroud)

它们出现在 selenium 对象中,这里是它们的在线 API 文档