【问题标题】:select_by_visible text not working properlyselect_by_visible 文本无法正常工作
【发布时间】:2015-12-13 08:48:21
【问题描述】:

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

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"

我做错了什么?

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    我认为您需要先移动到该元素,然后再选择它:

    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"))
    

    【讨论】:

    • 两个例子都不起作用,mouseMove 适用于 PhantonJS 吗?
    • @Thales 好的,你能用 Chrome 或 Firefox 试试同样的代码吗?另外,您能否分享一个指向您正在测试它的网站的链接?
    • 问题与网站有关(我无法分享链接)。我刚刚测试过:the-internet.herokuapp.com/dropdown,我的代码运行良好。我要调查一下我们的 html 端可能有什么
    • 我不知道发生了什么......突然开始工作:\。我应该删除这个问题吗?
    • @Thales 好的,继续。谢谢。
    猜你喜欢
    • 1970-01-01
    • 2016-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-01
    • 1970-01-01
    相关资源
    最近更新 更多