0

I am trying to write a test in Selenium, but for some reason my wdwait line of code in below method is failing with error message in title. When I comment the wdwait line, test passes normally. What am I missing?

One thing worth mentioning... in the same test, I have almost identical method for quantity dropbox and here wdwait works just fine. Both of the dropboxes are of the same "Select" type.

searchBoxDropdown is dropdown on amazon.com homepage next to the search bar

@FindBy(id="searchDropdownBox")
    WebElement searchBoxDropdown;
public void selectDropdownElement(String elementx) {
        wdwait.until(ExpectedConditions.elementToBeClickable(searchBoxDropdown));
        Select elementDropdown = new Select(searchBoxDropdown);
        elementDropdown.selectByVisibleText(elementx);
    }
@Test
    public void amazonTest() {
        amazonAddToCart.selectDropdownElement("Books");
        amazonAddToCart.inputSearchBox("Six of Crows Boxed Set");
        amazonAddToCart.clickBookLink();
        amazonAddToCart.selectHardcover();
        amazonAddToCart.selectQuantity(20);
        amazonAddToCart.clickGiftCheckbox();
        amazonAddToCart.clickAddToCart();
    }
<select aria-describedby="searchDropdownDescription" class="nav-search-dropdown searchSelect nav-progressive-attrubute nav-progressive-search-dropdown" data-nav-digest="k+fyIAyB82R9jVEmroQ0OWwSW3A=" data-nav-selected="0" id="searchDropdownBox" name="url" style="display: block; top: 2.5px;" tabindex="0" title="Search in">

I tried changing the method "elementToBeClickable" to "visibilityOfElements" but result was the same. Is there any similar method that allows WebElement to be selected that I could use maybe?

This solution is working the way I imagined:

public void selectDropdownElement(String elementx) {
        wdwait.until(ExpectedConditions.textToBePresentInElement(searchBoxDropdown, "All"));
        Select elementDropdown = new Select(searchBoxDropdown);
        elementDropdown.selectByVisibleText(elementx);
    }
1
  • 1
    Update the question with the text based HTML of the element. Commented Jul 15, 2023 at 20:41

1 Answer 1

0

I'm not sure you should check for selection or visibility in this case. Element in DOM is placed backward when search bar is brought to front, so method can return false negative result.

But your point is to select value, right? This code works for me (I select Automotive value)

driver.get("https://amazon.com");
WebDriverWait wdwait = new WebDriverWait(driver, 10);
By locator = new By.ById("searchDropdownBox");
WebElement dropdown = wdwait.until(ExpectedConditions.presenceOfElementLocated(locator));
Select elementDropdown = new Select(dropdown);
elementDropdown.selectByValue("search-alias=automotive-intl-ship"); //option value attribute

or in your kind of implementation

@FindBy(id="searchDropdownBox")
    WebElement searchBoxDropdown;
    
 
public void selectDropdownElement(String value) {
  Select elementDropdown = new Select(searchBoxDropdown);
  elementDropdown.selectByValue(value);
}

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

6 Comments

The main point is to select an option from dropdown menu. Whether it is by a value or other method is less important. But before that, I am trying to use @FindBy WebElement in combination with WebDriverWait method that involves using WebElement instead of locator. That was the reason I tried "elementToBeClickable" and "visibilityOfElements" methods. Not sure if there was any other option. I hope it makes sense.
You can init element by @FindBy, that doesn't matter. I'm sure that selectByValue would work in both cases with this dropdown.
But I need to create both new variable (in your example "locator") and new WebElement with FindBy right? I was hoping to kill 2 birds with one stone by only creating new WebElement with FindBy and using it for both wdwait and Select. I am still a beginner, so hopefully my comment is not too confusing. Appreciate your help.
Not sure you need to wait in this case. Try second code snippet I provided just now.
Yes, it's working without wait, thanks. If I still want to include wait, I just found this method is working in this case: wdwait.until(ExpectedConditions.textToBePresentInElement(searchBoxDropdown, "All"));
|

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.