2013-04-15 95 views
1

硒webdriver的單擊事件選擇的選項不能正常工作從選擇2下拉列表中選擇的選項。硒webdriver的單擊事件不工作從選擇2下拉

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

在這種情況下,如果我傳遞了正確的廣告客戶名稱,那麼它會打印找到的廣告客戶。但不能從該下拉菜單中選擇相同的廣告客戶。點擊後基本上沒有任何事情發生。

能否請你讓我知道我在這裏失蹤?

謝謝。

+0

sel_advertiser.select_by_visible_text(「advertisername」)也不起作用。 – user2030417

回答

0

沒有probleme實際。 網絡驅動器無法刷新下拉菜單。

,你可以看到你的下拉菜單的選項被選擇的最好的辦法是寫類似

option.submit(); 

不是refresh(),但submit()

PS:我有同樣的問題,我需要發送表單刷新下拉列表和所有複選框了。

+0

謝謝。如果我想這我得到「InvalidElementStateException:消息:u'Element不是一個形式,所以無法提交'。」的錯誤。 – user2030417

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() 
+0

非常感謝!我無法過去這麼久...... – jtanman

相關問題