简体   繁体   中英

select_by_visible text not working properly

I have this following block of code which allows me to successfully retrieve every value of a dropdown box:

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)

But when i try to select one of the values by using the text of each field using:

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

The following error shows:

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

What could i be doing wrong?

Thanks in advance.

I think you need to move to the element before selecting it:

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

Or using mouseMove() action:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM