使用Python版本的Selenium,是否可以點擊DOM中的某個元素並指定想要點擊它的座標? Java版本的方法clickAt
,實際上它正是我正在查找的內容,但無法在Python中找到等價物。硒 - 在特定位置點擊
11
A
回答
1
我沒有親自使用這種方法,而是通過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,])
它們出現在硒對象中,這裏是它們的online API documentation。
4
您感到困惑的原因是clickAt
是一箇舊的v1(Selenium RC)方法。
WebDriver有一個略有不同的概念,'Actions'。
具體而言,Python綁定的'Actions'生成器生效here。
clickAt
命令的想法是點擊某個位置相對於到一個特定的元素。
在WebDriver中使用'Actions'構建器可以實現同樣的效果。
希望這updated documentation可以幫助。
22
這應該做到這一點!即你需要使用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()
這將從按鈕的左上角右移動鼠標5個像素下來,5個像素我覺得很幸運。那麼它會click()
。
請注意,您必須使用perform()
。否則什麼都不會發生。
相關問題
- 1. 在點擊位置
- 2. 如何使用phpUnit/selen模擬點擊特定位置?
- 3. 修復按鈕在特定滾動位置點擊滾動位置
- 4. 如何使特定位置在UITableView中的UiLabel中可點擊?
- 5. 使圖像在特定位置可點擊
- 6. Python:如何在python的特定位置點擊屏幕,MAC OS
- 7. Pygame在GUI上點擊特定位置時發出聲音
- 8. Swift:在特定位置點擊UILabel上的動作
- 9. 僅發送點擊在指定位置
- 10. 在特定位置插入XML節點
- 11. 確定點擊柵格中點擊的位置,在R
- 12. 點擊硒
- 13. 點擊()不硒
- 14. 在UIImage中點擊位置
- 15. 在特定位置
- 16. 在特定位置
- 17. Python和硒 - 無法找到並點擊此特定元素由於它隨機在網站上的位置
- 18. 在硒中定位WebElements的子節點
- 19. auto使用javascript/html點擊iframe上的特定位置
- 20. javascript模擬鼠標點擊特定位置
- 21. 模擬點擊頁面中的特定位置
- 22. jQuery的編程點擊特定的X Y位置DIV
- 23. 如何使用鼠標點擊到Mac上的特定位置?
- 24. 單擊特定位置上的ImageView
- 25. 量角器雙擊特定位置
- 26. 更改位置點擊點
- 27. 使用SQL在特定位置插入特定的XML節點
- 28. 不能點擊()在硒Python
- 29. 問題在點擊硒chromedriver
- 30. 點擊style =「display:block;」在硒webdriver
太棒了!他們屬於哪個班級? – davids
他們在硒對象。實際上我剛剛在網上找到了他們的API文檔 - 更新了答案。 – Ewan
還有一個問題。你怎麼實際使用它?我習慣使用webdrivers對象,但從未使用過這個 – davids