select_by_visible文本无法正常工作

时间:2015-09-16 16:36:35

标签: python python-2.7 selenium selenium-webdriver phantomjs

我有以下代码块,它允许我成功检索下拉框的每个值:

def extract_sct_projects(driver, base_url):
    driver.get(base_url)
    driver.find_element_by_id('dropdown_id').click()
    time.sleep(2)
    element = driver.find_element_by_id('list_of_objects')
    select = Select(element)
    for o in select.options:
        print o.get_attribute("text")

base_url = 'http://localhost'
phantom_js = 'C:\\phantomjs-2.0.0\\bin\\phantomjs.exe'
driver = webdriver.PhantomJS(executable_path=phantom_js)

extract_sct_projects(driver, base_url)

但是当我尝试使用每个字段的文本选择其中一个值时使用:

for o in select.options:
    select.select_by_visible_text(o.get_attribute("text"))

以下错误显示:

"errorMessage":"Element is not currently visible and may not be manipulated"

我可能做错了什么?

提前致谢。

1 个答案:

答案 0 :(得分:0)

我认为你需要在选择之前移动元素:

for option in select.options:
    driver.execute_script("arguments[0].scrollIntoView();", option)
    select.select_by_visible_text(option.get_attribute("text"))

或使用mouseMove()操作:

actions = ActionChains(driver)
for option in select.options:
    actions.move_to_element(option).perform()
    select.select_by_visible_text(option.get_attribute("text"))