2013-04-15 96 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

相关问题