0

I am trying to open a webpage and select options before submitting the form. The issue I am having is being able to selecting more than one option in the select box. Eventually I want to be able to deselect an option as well and submit the the form.

A snippet of the relevant html and python code is below

Snippet of html:

<select size="10" name="ctl00$MainContent$lbCommodity" multiple="multiple" id="ctl00_MainContent_lbCommodity">
 <option value="101">Wheat - HRW</option>
 <option value="102">Wheat - SRW</option>
 <option value="103">Wheat - HRS</option>
 <option value="104">Wheat - White</option>
 <option value="105">Wheat - Durum</option>

Python code:

driver = webdriver.Chrome()
driver.get("http://apps.fas.usda.gov/esrquery/esrq.aspx")
driver.find_element_by_xpath("//select[@name='ctl00$MainContent$lbCommodity']/option[@value='801']").click()
asked Feb 18, 2015 at 14:37
1
  • Hi Moj: can you give more detail about what goes wrong when you run your test, and what your expected result was? If there is an error, some detail from that would be useful. Commented Apr 20, 2015 at 16:14

2 Answers 2

1
  1. read up on Select object - http://selenium-python.readthedocs.org/en/latest/api.html?highlight=select#module-selenium.webdriver.support.select

  2. Use IDs or names, or even CSS before XPATH. Best practices, explained here

answered Feb 18, 2015 at 14:54
1
0

Hope this below code helps:

driver=new firefoxdriver();
//create select class object for selecting multiple select box field
Select s=new Select(driver.findElement(by.cssSelector("select[multiple='multiple']"));
/*you can use selectByVisibleText() predefined method of Select class for selecting multiple values*/
s.selectByVisibleText("Wheat - HRW");
s.selectByVisibleText("Wheat - White");
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Jul 10, 2016 at 18:16

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.