在Splinter(和Selenium)中处理隐藏的选择元素

0 投票
1 回答
1717 浏览
提问于 2025-04-18 17:22

我在使用splinter的时候遇到了一个问题。我尝试运行这段代码:

# -*- coding: utf-8 -*-
from splinter import Browser

browser = Browser()
browser.visit('https://passport.yandex.com/registration/mail')
browser.find_by_name("hint_question_id").click()
browser.select("hint_question_id","12")

但是出现了这个错误:

selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace: 
at fxdriver.preconditions.visible (file:///c:/users/dm/appdata/local/temp/tmppiwmlb/extensions/fxdriver@googlecode.com/components/command_processor.js:8791:5)

我又尝试用selenium:

# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.support.ui import Select

driver = webdriver.Firefox()
driver.get("https://passport.yandex.com/registration/mail")

driver.find_element_by_name("hint_question_id").click()

# navigate to the page
select = Select(driver.find_element_by_tag_name("select"))
print select.options
print [o.text for o in select.options] # these are string-s
select.select_by_index('2')

但还是出现了同样的错误:

selenium.common.exceptions.ElementNotVisibleException: Message: u'Element is not currently visible and so may not be interacted with' ; Stacktrace: 
at fxdriver.preconditions.visible (file:///c:/users/dm/appdata/local/temp/tmpzjqnnp/extensions/fxdriver@googlecode.com/components/command_processor.js:8791:5)

我到底哪里做错了呢?谢谢。

1 个回答

0

我找到了解决办法。这个网站使用了伪选择器。对于Splinter来说,之后需要写:

browser.find_by_name("hint_question_id").click()

这样就可以了。

browser.find_by_css("li[role=\"presentation\"]")[1].click()
browser.find_by_id("hint_answer").fill(firstname)

效果很好。

撰写回答