如何处理隐藏的选择下拉菜单

发布于 2025-01-13 16:44:36 字数 1589 浏览 1 评论 0 原文

Faced the situation when using Select class, selenium complains that "element not interactable".

HTML 代码

        driver.findElement(By.cssSelector("div.select")).click();
        Select select = new Select(driver.findElement(By.id("phonePrefix")));
        select.selectByIndex(5);

来自控制台的输出

无论如何,我找到了一种通过循环列表并按文本选择来从下拉列表中选择元素的方法......

driver.findElement(By.cssSelector("div.select")).click();
        
        WebElement drop = driver.findElement(By.cssSelector("select"));
        
        List<WebElement> countryies = driver.findElements(By.cssSelector("ul[class='select-options'] li"));
        for(WebElement i:countryies) {
            System.out.println(i.getText());
            if(i.getText().contains("+93 Afghanistan")) {
                i.click();
                break;
            }
        }

但问题是为什么我不能使用 Select 类和select.getFirstSelectedOption() 以及如何处理这个问题? Thnx

P.s 还尝试了一些显式等待。

    driver.get("https://banking.idram.am/Account/login");
        driver.findElement(By.cssSelector("div.select")).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.id("phonePrefix")));
        Select select = new Select(driver.findElement(By.id("phonePrefix")));
        select.selectByIndex(5);

输出

Faced the situation when using Select class, selenium complains that "element not interactable".

HTML code

        driver.findElement(By.cssSelector("div.select")).click();
        Select select = new Select(driver.findElement(By.id("phonePrefix")));
        select.selectByIndex(5);

Output from the console

anyway I found a way to select element from dropdown by looping through the List and selecting by text.....

driver.findElement(By.cssSelector("div.select")).click();
        
        WebElement drop = driver.findElement(By.cssSelector("select"));
        
        List<WebElement> countryies = driver.findElements(By.cssSelector("ul[class='select-options'] li"));
        for(WebElement i:countryies) {
            System.out.println(i.getText());
            if(i.getText().contains("+93 Afghanistan")) {
                i.click();
                break;
            }
        }

But the question is why i cannot use Select class and select.getFirstSelectedOption() and how to handle this?
Thnx

P.s Also tried some explicit wait.

    driver.get("https://banking.idram.am/Account/login");
        driver.findElement(By.cssSelector("div.select")).click();
        wait.until(ExpectedConditions.elementToBeClickable(By.id("phonePrefix")));
        Select select = new Select(driver.findElement(By.id("phonePrefix")));
        select.selectByIndex(5);

Output

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沧桑㈠ 2025-01-20 16:44:36

根据您使用 HTML 代码发布的图像,您应该以这种方式从下拉列表中选择项目:

public class DropDownDemo {
    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "YOUR_PATH_TO_DRIVER_EXE_HERE");
        WebDriver driver = new ChromeDriver();
        driver.get("https://banking.idram.am/Account/login");
        driver.manage().window().maximize();
        WebElement divSelect = new WebDriverWait(driver, Duration.ofSeconds(5), Duration.ofMillis(100)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.select")));
        divSelect.click();
        WebElement option = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofMillis(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[text()='+93 Afghanistan']")));
        option.click();
        Thread.sleep(5000); // only to see the result
        driver.close();
    }
}

如果使用 Selenium 3,请删除 Duration.ofXXX() 并直接将数值传递给构造函数。

为什么我不能使用 Select 类和 select.getFirstSelectedOption() 以及如何处理这个问题?

<select class="phonePrefix select-hidden" required="required" name="phonePrefix" id="phonePrefix">
    ...
</select>

正如您从发布的代码片段中看到的,Select 元素使用 select-hidden 类来实现下拉菜单。因此,Selenium 不能与其相互作用。此应用程序使用的内容称为Bootstrap Dropdown。此外,确定使用哪一个(即 select 与 ul/li)的最简单方法是检查页面并选择其中一个选项。当您执行此操作时,它将转到 li 元素而不是 option 元素。

Based on the image you posted with the HTML code, you should select the item from the dropdown in this manner:

public class DropDownDemo {
    public static void main(String[] args) throws Exception {
        System.setProperty("webdriver.chrome.driver", "YOUR_PATH_TO_DRIVER_EXE_HERE");
        WebDriver driver = new ChromeDriver();
        driver.get("https://banking.idram.am/Account/login");
        driver.manage().window().maximize();
        WebElement divSelect = new WebDriverWait(driver, Duration.ofSeconds(5), Duration.ofMillis(100)).until(ExpectedConditions.elementToBeClickable(By.cssSelector("div.select")));
        divSelect.click();
        WebElement option = new WebDriverWait(driver, Duration.ofSeconds(10), Duration.ofMillis(10)).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//li[text()='+93 Afghanistan']")));
        option.click();
        Thread.sleep(5000); // only to see the result
        driver.close();
    }
}

If using Selenium 3, remove the Duration.ofXXX() and just pass the numeric values directly to the constructor.

why I cannot use Select class and select.getFirstSelectedOption() and how to handle this?

<select class="phonePrefix select-hidden" required="required" name="phonePrefix" id="phonePrefix">
    ...
</select>

As you can see from the posted snippet, Select element is using select-hidden class to implement the dropdown. Therefore, Selenium cannot interact with it. What this application is using is called a Bootstrap Dropdown. Also, the easiest way to figure out which one to use (i.e. select vs. ul/li) is by inspecting the page and select one of the options. When you do this, it goes to the li element rather than to the option element.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文