python - 碎片:可见下拉菜单可点击但不可选择

标签 python css selenium selenium-webdriver splinter

我正在尝试通过 splinter 从模态框的下拉菜单中选择内容。我很容易找到这个下拉菜单,例如:

(Pdb) dropdown = next(i for i in my_browser.find_by_xpath('//select[@name="existing.widgets.user:list"]') if i.visible)

(我正在处理的页面实际上有多个相同的模态,所以我必须获得当前可见的模态。唉......)

可以点击下拉列表:

(Pdb) dropdown.visible
True
(Pdb) dropdown.click()  //succeeds and displays menu
(Pdb)

...但是尝试选择它失败了,即使它应该是可见的!

(Pdb) dropdown.select('my_val')
*** ElementNotVisibleException: Message: Element is not currently visible and so may not be interacted with
Stacktrace:
    at fxdriver.preconditions.visible (file:///tmp/tmp6tSmOc/extensions/fxdriver@googlecode.com/components/command-processor.js:9587)
    at DelayedCommand.prototype.checkPreconditions_ (file:///tmp/tmp6tSmOc/extensions/fxdriver@googlecode.com/components/command-processor.js:12257)
    at DelayedCommand.prototype.executeInternal_/h (file:///tmp/tmp6tSmOc/extensions/fxdriver@googlecode.com/components/command-processor.js:12274)
    at DelayedCommand.prototype.executeInternal_ (file:///tmp/tmp6tSmOc/extensions/fxdriver@googlecode.com/components/command-processor.js:12279)
    at DelayedCommand.prototype.execute/< (file:///tmp/tmp6tSmOc/extensions/fxdriver@googlecode.com/components/command-processor.js:12221)
(Pdb) dropdown.visible
True  // what???
(Pdb)

我很确定 select 的参数是正确的,所以我不知道这里发生了什么。

如果一切都失败了,我可以用 xpath 做些聪明的事情吗?或者我是否需要尝试以其他方式查找元素/与元素交互?

HTML 情况的部分截图:http://pasteboard.co/1I30ljRl.png

最佳答案

事实证明,多重模态让我着迷。

鉴于我的名称为 'existing.widgets.user:list' 的下拉列表和所需的 my_val,失败的 select 调用,在其来源,有:

find_by_xpath('//select[@name="%s"]/option[@value="%s"]' % (dropdown['name'], my_val).click()

现在,事实证明这个 find_by_xpath 实际上返回了多个/重复的选项,可能来自多个模态!

(Pdb) blah=my_browser.find_by_xpath('//select[@name="%s"]/option[@value="%s"]' % (dropdown['name'], my_val)
(Pdb) blah
[<splinter.driver.webdriver.WebDriverElement object at 0x7f7205ff3750>, <splinter.driver.webdriver.WebDriverElement object at 0x7f7205ff39d0>, <splinter.driver.webdriver.WebDriverElement object at 0x7f7205ff38d0>, <splinter.driver.webdriver.WebDriverElement object at 0x7f7205ff3610>]
(Pdb) [bl.value for bl in blah]
[u'my_val', u'my_val', u'my_val', u'my_val']
(Pdb) [bl.visible for bl in blah]
[False, True, False, False]

我想我可以通过正确的模式(例如,使用特定的表单类)而不是通过整个页面访问选项来解决这个问题;即,从类似的东西开始

form = my_browser.find_by_xpath('//form[@id="{0}"]'.format(my_current_modal))

...然后尝试

dropdown = form.find_by_xpath(...)
dropdown.select(...)
etc.

但是导致了同样的问题;仍然找到多个元素,但仍然失败!因此,我求助于可见性检查,这确实有效:

opts = form.find_by_xpath('//select[@name="%s"]/option[@value="%s"]' % (dropdown['name'], my_val)
next(opt for opt in opts if opt.visible).click()

也许我在这里仍然缺少一些东西......甚至是 Splinter 的一些错误,但至少它有效!

关于python - 碎片:可见下拉菜单可点击但不可选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28974231/

相关文章:

python - 如何获取有关Python "the specified module could not be found"的更多详细信息

angularjs - Angular Material md-grid-list 在页面刷新时重叠

jquery - 预加载 div 的所有背景图像

c# - 谷歌搜索按钮点击 Selenium

python - 在 python 2.7 中使用 Selenium 出现 StaleElementReferenceException 错误

python - 素数 python

python - 不成功的 TensorSliceReader 构造函数 : Failed to find any matching files for bird-classifier. tfl.ckpt-50912

html - 如何仅在悬停事件后才在 Nivo Slider 中显示标题?

Selenium WebDriver 工作但很慢(Java)

python - 使用 [-1] 获取列的最后一行?