1

selenium webdriver click 事件不适用于从 select2 下拉列表中选择的选项。

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 中没有这个 hack,但我需要 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

实际上没有问题。网络驱动程序无法刷新下拉列表。

你可以看到你的下拉选项被选中的最好方法是写类似

 option.submit();

不是一个refresh(),而是一个submit()

PS:我遇到了同样的问题,我需要发送表单来刷新下拉列表和所有复选框。

于 2013-04-16T06:45:27.510 回答