简体   繁体   中英

Click all values in a drop down list using selenium

I'm scrapping a website

http://tabnet.saude.prefeitura.sp.gov.br/cgi/deftohtm3.exe?secretarias/saude/TABNET/AIHRD08/AIHRDNET08.def

I need to select all values in the field Períodos Disponíveis . I created a for loop to do it because the Xpath of field follow a sequence:

element = browser.find_element_by_xpath('//*[@id="A"]')
all_options = element.find_elements_by_tag_name("option")
    for option in all_options:
        option.click()
browser.find_element_by_xpath('//*[@id="A"]/option[1]').click()

But this is too slow, how can I make this faster?

I just tested and I have implemented keyboard shortcut. After identifying the element:

element = browser.find_element_by_xpath('//*[@id="A"]')

Use send keys

element.send_keys(Keys.SHIFT + Keys.END)

Assumption is that you have already declared Keys as:

from selenium.webdriver.common.keys import Keys

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