1

I need Selenium to choose menu with VALUE that I obtained from crawling. Here is the portion of HTML code of Dropdown menu:

<select class="graySelect" name="sch_bub_nm" id="sch_bub_nm" 
title="Case Number" onchange="onChangeBub();">
<option value="000100">Case1</option>
<option value="000200">Case2</option>
<option value="000201">Case3</option>
.
.
.

Here is the code I wrote so far:

def MenuChoose():
   driver.find_element_by_css_selector('#sch_bub_nm').click()
   driver.find_element_by_xpath("//*[@id="sch_bub_nm"]/option[1]")

As you can see, I tried to choose the menu, and I got stuck as xpath showed no value that I can direct code to.

2 Answers 2

2

You need to create a select element in order to interact with it.

from selenium.webdriver.support.ui import Select

select = Select(driver.find_element_by_css_selector('#sch_bub_nm'))
select.select_by_index(1)  # Choose the position you want

Check the selenium-python documentation to see more options for select.

Sign up to request clarification or add additional context in comments.

1 Comment

I'm guessing both of the comment works, but it looks like I have to select iframe that contains dropdown menu within webpage first for this to work. Thanks anyways!
1

You should use Select to get the dropdown value. I have given 3 options to select value.

from selenium.webdriver.support.select import Select
select=Select(driver.find_element_by_id("sch_bub_nm"))
select.select_by_index(1) #select index value
select.select_by_visible_text("Case2") # select visible text
select.select_by_value("000201") # Select option value

Let me know if this work.

1 Comment

I'm guessing both of the comment works, but it looks like I have to select iframe that contains dropdown menu within webpage first for this to work. Thanks anyways!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.