0

enter image description here

<div class="left">
<select id="hidebox" class="ays-ignore" size="8">
<ul><option value="103249">260 - OMAHA 30TH ST</option></ul> 
<ul><option value="103266">540 - UNION CITY</option></ul>
<ul><option value="3">800 - BETHLEHEM</option></ul>
<ul><option value="103271">430 - DALLAS SOUTH</option></ul>
<ul><option value="103270">410 - DALLAS</option></ul>
<ul><option value="100966">530 - TRACY</option></ul>
<ul><option value="103272">420 - FORT WORTH</option></ul>
<ul><option value="97224">820 - QUAKERTOWN</option></ul>
<ul><option value="103324">520 - SACRAMENTO</option></ul>
</select>
</div>

Hello, I'm trying click on an element based on the label "820 - QUAKERTOWN" and my approach is either incorrect or im missing something cause when i step thru, i see the entire list being returned but the required value is not clicked. The xpath is correct and appears to return the correct list of records, but the click is not being applied. My apologies if I use the wrong verbiage im new to Selenium and Java. I excluded the driver and get() website code, those lines were not revelant to my question. Please suggest other ideas if im using the wrong approach. I somewhat hit the wall.

My expectation is that the row can be clicked using the label text such as "800 - BETHLEHEM" or any other from list. I did try the unique xpath approach and it worked, but my test fails when I run it against other environments where the option value is different.

Thank you for any help y'all can provide. Example from a successful clicked when I used a unique xpath which I rather not used because of the environment reason.

enter image description here

package com.usc.uscspace.test.customer;

import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.testng.annotations.Test;

import java.util.List;

public class CreateTest extends AbstractTestClass {

    final static String legacyWarehouse = "800 - BETHLEHEM";


    @Test
    public void createCustomerTest() {
        List<WebElement> pushWhseList = driver.findElements(By.xpath(".//*[@id='hidebox']"));

        int total_node = pushWhseList.size();

        for (int i = 0; i < total_node; i++) {
            String list = pushWhseList.get(i).getText();
            if (list.contains(legacyWarehouse)) {
                pushWhseList.get(i).click();
                break;
            }
        }
    }

}
Bobby Mor
  • 77
  • 1
  • 7
  • Would you mind formatting your code properly? – Hubert Grzeskowiak Nov 08 '17 at 04:36
  • Plenty of questions about that on StackOverflow, e.g.: https://stackoverflow.com/questions/20138761/how-to-select-a-dropdown-value-in-selenium-webdriver-using-java https://stackoverflow.com/questions/12940592/how-to-select-an-item-from-a-dropdown-list-using-selenium-webdriver-with-java – Hubert Grzeskowiak Nov 08 '17 at 04:44

1 Answers1

1

Could you try this:

Select mySelect = new Select(driver.findElement(By.xpath(".//*[@id='hidebox']")));
mySelect.selectByVisibleText(legacyWarehouse);

or directly:

driver.findElement(By.xpath(".//ul/option[text()='"+legacyWarehouse+"']")).click()
j.barrio
  • 1,006
  • 1
  • 13
  • 37
  • Thank you j.barrio. The first suggestion worked like a charm and second suggestion also worked. Thanks! – Bobby Mor Nov 08 '17 at 13:56