2013-05-29 17 views
11

SeleniumのPython版を使用すると、DOM内のいくつかの要素をクリックして、クリックしたい座標を指定することは可能ですか? JavaバージョンにはclickAtというメソッドがありますが、これは実際に私が探しているものですが、Pythonではこれを見つけることができません。セレン - 特定の位置でクリック

答えて

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があります。

+0

素晴らしい!彼らはどのクラスに属していますか? – davids

+0

彼らはセレンのオブジェクトに入っています。私は実際にAPIドキュメントをオンラインで見つけました。答えを更新しています。 – Ewan

+0

もう一度質問します。どのように実際にこれを使用していますか?私はwebdriversオブジェクトで作業するのに慣れていますが、これを使用したことはありません – davids

4

あなたが混乱している理由は、clickAtは古いv1(Selenium RC)メソッドです。

WebDriverの概念は少し異なり、'Actions'です。

具体的には、Pythonバインディング用の「アクション」ビルダーは、ライブhereです。

clickAtコマンドの考え方は、特定の要素の特定の位置相対でクリックすることです。

「アクション」ビルダーを使用してWebDriverでも同じことが達成できます。

うまくいけば、これは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()です。それ以外は何も起こりません。

関連する問題