Selecting two options in the drop down menu with Selenium + Python does not work

Asked

Viewed 262 times

0

from selenium import  webdriver
from time import sleep
from urllib.request import Request,urlopen
import pandas as pd
from selenium.webdriver.support.ui import Select        
from selenium.webdriver.support.ui import Select  

url = "https://www.viprbrc.org/brc/vipr_genome_search.spg?method=ShowCleanSearch&decorator=flavi_dengue"
browser = webdriver.Chrome(xxxx)
browser.get(url)
sleep(20)


box_drp_x= browser.find_element_by_id("hosts").click()
box_dpr = browser.find_element_by_id("hostsBox")
human_host = Select(box_drp)
human_host.select_by_value("Human")

inserir a descrição da imagem aqui

How do I select two options in the drop down menu (Human and Mosquito)? My code runs but does not select, which may be occurring?

1 answer

0


Good afternoon, I’m a python beginner and tried several ways to use the select option but I did not succeed because he insists that the button is not visible or can not interact with it, but with the following code I was able to type the two options in the field and select them:

from selenium import webdriver
from time import sleep
from selenium.webdriver.common.keys import Keys

url = "https://www.viprbrc.org/brc/vipr_genome_search.spg?method=ShowCleanSearch&decorator=flavi_dengue"
browser = webdriver.Chrome()
browser.get(url)
sleep(3)
browser.find_element_by_xpath('//div[@id="hosts"]').click()
input_field = browser.find_element_by_xpath('//input[@value="Choose a Host..."]')
hosts = ['Human', 'Mosquito']

for value in hosts:
    input_field.send_keys(value)
    input_field.send_keys(Keys.ENTER)

First of all I clicked on the div hosts and with it using : Selenium.webdriver.common.Keys I sent the texts Human and Mosquito to the input field pressing Enter right after, apparently something hides the actual selection.

EDIT

The problem of not being able to select select I believe it is because it is with the "display-style: None", even if I run to change it to "block" it can not make the selection, looking better the source saw that the selection actually occurs here:

Lugar correto onde é feita a escolha

What you can see with this code:


from selenium import webdriver
from time import sleep

url = "https://www.viprbrc.org/brc/vipr_genome_search.spg?method=ShowCleanSearch&decorator=flavi_dengue"
browser = webdriver.Chrome()
browser.get(url)
sleep(3)
menu = browser.find_element_by_xpath('//div[@id="hosts"]')
menu.click()
hosts = menu.find_elements_by_xpath('//div/ul/li[@class="active-result"]')
for value in hosts:
    if value.text == 'Human':
        value.click()
    else:
        pass

In this second example you would need only change to click the two values

Browser other questions tagged

You are not signed in. Login or sign up in order to post.