1

select2 ドロップダウンから選択されたオプションに対して、selenium webdriver クリック イベントが機能しません。

sel_advertiser = Select(self.driver.find_element_by_id("brand_option"))
for option in sel_advertiser.options:
name = str(option.get_attribute("text"))
if name == advertiser_name:
    print "Found advertiser"
    option.click()

この場合、正しい広告主名を渡せば、印刷の広告主が見つかりました。ただし、そのドロップダウンから同じ広告主を選択していません。基本的に、クリック後は何も起こりません。

ここで何が欠けているのか教えてください。

ありがとう。

4

2 に答える 2

1

ActionChains クラスを使用して要素に移動し、クリックする必要がありました。次に、Select2 要素が Firefox と PhantomJS で開きます。Chrome でこのハックがなくても機能しましたが、PhantomJS のサポートが必要でした。

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait

#Click on the element to open the dropdown
el = driver.find_element_by_id('id_to_element')
actions = ActionChains(driver)
actions.move_to_element(el)
actions.click()
actions.perform()

#Click on the correct item within the dropdown, waiting for it to load
item_to_select = 'Some text in select'

xpath = '//div[@class="select2-result-label" and' +\
    ' text()[contains(.,"%s")]]' % (item_to_select)

wait = WebDriverWait(driver, 10)
elm = wait.until(
    EC.element_to_be_clickable(
        (
            By.XPATH,
            xpath
        )
    ),
    'waiting for %s to be clickable' % (xpath)
)
elm.click()
于 2014-03-12T16:10:01.100 に答える
0

実際には問題ありません。Web ドライバーがドロップダウンを更新できません。

ドロップダウンのオプションが選択されていることを確認できる最良の方法は、次のように書くことです

 option.submit();

ではなくrefresh()submit()

PS:同じ問題があり、フォームを送信してドロップダウンとすべてのチェックボックスも更新する必要がありました。

于 2013-04-16T06:45:27.510 に答える