Selenium - Click at certain position

后端 未结 4 1632
既然无缘
既然无缘 2020-12-08 15:13

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 met

相关标签:
4条回答
  • 2020-12-08 15:54

    The reason you are getting confused is clickAt is an old v1 (Selenium RC) method.

    WebDriver has a slightly different concept, of 'Actions'.

    Specifically, the 'Actions' builder for the Python bindings live here.

    The idea of the clickAt command is to click at a certain position relative to a particular element.

    The same is achievable within the WebDriver, using the 'Actions' builder.

    Hopefully this updated documentation can help.

    0 讨论(0)
  • 2020-12-08 16:01

    I've not personally used this method, but looking through the source code of selenium.py I've found the following methods that look like they'd do what you want - They look to wrap 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.

    0 讨论(0)
  • 2020-12-08 16:02

    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.

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

    Notice that you must use perform(). Else nothing will happen.

    0 讨论(0)
  • 2020-12-08 16:12

    You can perform the task with the Action chains in the python specially with Edge browser:

    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:

    button_xpath  = '//xapth...' 
    button = driver.find_element_by_xpath(button_xpath)
    driver.execute_script("arguments[0].click();", button)
    
    0 讨论(0)
提交回复
热议问题