简体   繁体   中英

Selecting elements from “div” dropdown - Selenium

I have a "div" dropdown, not "select". So my function works fine everywhere except dropdows which open not from the dropdown button place, but from above (because dropdown is placed at the bottom of the page) to prevent... page scrolling i guess? Maybe the problem is that it opens too fast or too slow or what, so function usually clicks not the needed element but one of the closest to it. Mostly it works this way if element is not at the beginning of dropdown so it is scrolled to the element. Any suggestions what can be made?

The best way i found is to use actions, splitting moving to element and clicking into 2 rows (works much worse if writing in 1 row). By the way, "waitVisibilityOfElement(By)" is a function with webdriver wait for expected conditions

public void selectFromDropdown(By by) {
        Log.debug("selecting from dropdown by" + by);
        waitVisibilityOfElement(by);
        Actions actions = new Actions(wrappedWebDriver);
       actions.moveToElement(wrappedWebDriver.findElement(by)).perform();
        Log.debug("clicking dropdown item");
        wrappedWebDriver.findElement(by).click();
}

I expect needed element to be clicked, But usually it clicks another one

Ok with some help i came to a final decision which is the best way to select dropdown filters without using any .sleep() functions if other simple ways do not work for you.

public void selectFromDropdown(By by) {
        Log.debug("selecting from dropdown by" + by);
        WebElement eleV = wrappedWebDriver.findElement(by);

        waitVisibilityOfElement(by);

        JavascriptExecutor js = (JavascriptExecutor) wrappedWebDriver;
        js.executeScript("arguments[0].scrollIntoView();", eleV);

        Actions actions = new Actions(wrappedWebDriver);
        actions.moveToElement(wrappedWebDriver.findElement(by)).perform();
        Log.debug("clicking dropdown item");
        wrappedWebDriver.findElement(by).click();
    }

Did you try to add fluet wait before clicking? That can be helpful. Also would be great if you can share information about DOM in that question? That is a sample for java, but I think that can help you in js too.

Wait<WebDriver> wait = new FluentWait<WebDriver>(driver)
    .withTimeout(30, SECONDS)
    .pollingEvery(5, SECONDS)
    .ignoring(NoSuchElementException.class);

Did you try to scroll to the expected element value before move mouse on it and click. Hope following code can help:

public void selectFromDropdown(By by) {
    Log.debug("selecting from dropdown by" + by);
    var eleValue = wrappedWebDriver.findElement(by);
    Log.debug("Scroll to element");
    browser.executeScript("arguments[0].scrollIntoView();", eleValue);
    waitVisibilityOfElement(by);
    Actions actions = new Actions(wrappedWebDriver);
    actions.moveToElement(eleValue).perform();
    Log.debug("clicking dropdown item");
    eleValue.click();
}

If the you are trying to click a static value, you can directly click the value from the dropdown

you can try this

driver.findElement(By.xpath("THE VALUE FROM DROPDOWN")).click();

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