Selenium - Click at certain position

后端 未结 4 1672
既然无缘
既然无缘 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 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.

提交回复
热议问题