简体   繁体   English

从“ div”下拉列表中选择元素-硒

[英]Selecting elements from “div” dropdown - Selenium

I have a "div" dropdown, not "select". 我有一个“ div”下拉列表,而不是“ 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. 如果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 顺便说一句,“ waitVisibilityOfElement(By)”是webdriver等待预期条件的函数

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. 好的,在我的帮助下,我做出了最终决定,这是选择下拉过滤器而不使用任何.sleep()函数的最佳方法,如果其他简单方法对您不起作用。

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? 如果您可以在该问题中共享有关DOM的信息,那也很好吗? That is a sample for java, but I think that can help you in js too. 这是Java的示例,但我认为这也可以帮助您使用js。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM