使用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().否则什么都不会发生.
我个人没有使用过这个方法,但是通过查看源代码,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 文档。