简体   繁体   中英

selenium webdriver java dropdown box

need some help on this. Trying to learn Java and selenium at the same time isn't good but has to be done :(

I am trying to test a drop down spots box which contains:

<select name="sport" id="sport">
 <option value="1">Soccer</option>
 <option value="2">Basketball</option>
 </select>

I want to perform the follow: 1: Assert box is present and its text is Soccer and Basketball 2: Click Basketball and then click Soccer again. 3: Assert the changes in the table below this box. (Have this done I think but need the above)

Code so far:

    Select sportDropdown = new Select(webBrowser.findElement(By.id("sport")));
    sportDropdown.selectByVisibleText("Soccer");
    assertEquals(sportDropdown, "Soccer");

Error received:

java.lang.AssertionError: Expected :org.openqa.selenium.support.ui.Select@278806c4 Actual :Soccer

I have no idea where this "Expected " value is coming from so any pointers would be great folks.

Note this is all in Java so please refrain from submitting code help in C#. Makes my life harder :(

Thanks J

You'll need to get the text of the selected option for your assert statement to work. Currently you are asking whether a WebElement object is equal to a String .

Select sportDropdown = new Select(webBrowser.findElement(By.id("sport")));
sportDropdown.selectByVisibleText("Soccer");
assertEquals(sportDropdown.getFirstSelectedOption().getText(), "Soccer");

For anyone whose value equals the text value use the get attribute. It worked for me. May be work for someone else.

Select sportDropdown = new Select(driver.findElement(By.id("id"))); sportDropdown.selectByValue("Value"); Thread.sleep(3000);//This was needed since the page is refreshed whenever the value is selected. System.out.println(driver.findElement(By.id("id")).getAttribute("value"));

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