有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法单击使用Selenium WebDriver创建的<div>下拉列表

HTML代码

<div qxselectable="off" >
<div qxselectable="off" >
<div tabindex="1" qxselectable="off" >
<div tabindex="1" qxselectable="off" >
<div style="overflow: hidden; >Discrepancy Type*</div>
<div class="qx-input-required" tabindex="7" ">    
<input class="qx-abstract-field qx-placeholder-color" >
//On Below button there is one dropdown button on which i want to click but i cannot 
<div class="qx-button" qxselectable="off" >
<div qxselectable="off" qxanonymous="true" ></div>
</div>
</div>
</div>
<div tabindex="1" qxselectable="off" >
</div>
</div>
<div class="qx-outSet" qxse..

Java代码

WebElement element = wd.findElement(By.className("qx-input-required"));
Actions actions = new Actions(wd);
actions.moveToElement(element).click().perform();
wd.findElement(By.xpath(".//*[@id='demindoRoot']/div[3]/div[2]/div[1]/div/div[2]/div[2]/div/div")).click(); // link through which i try to click 
Thread.sleep(1000);

我还尝试了下面提到的代码

wd.findElement(By.xpath("//div[@class='qx-button'"));

enter image description here

错误

Unable to locate element:


共 (3) 个答案

  1. # 1 楼答案

    WebDriverWait wait = new WebDriverWait(d, 10);
        WebElement val = wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath()));
        val.click();
    
    
        for(int i=0;i<=100;i++)
        {
    
            String value=val.getText();
            System.out.println("value is = "+value);
            String value1 = "Your dropdown value";
            if(value.equalsIgnoreCase(value1))
            {
                System.out.println("Got your dropdown value ");
                a.sendKeys(Keys.ENTER).build().perform();
                break;
            }
            a.sendKeys(Keys.DOWN,Keys.DOWN).build().perform();
            Thread.sleep(3000);
            a.sendKeys(Keys.ENTER).build().perform();
        }
    

    xpath应该是您的下拉框,而不是您的下拉值

  2. # 2 楼答案

    action.moveToElement(element).moveToElement(driver.findElement(By.Xpath(<Your path here>))).click().build().perform();
    

    这会像用户那样执行操作。用户首先导航到菜单,打开它,然后导航到要单击的元素。查看此问题了解更多详细信息: https://stackoverflow.com/a/17294390/3537915

  3. # 3 楼答案

    基本上,当我们使用任何使用javascript开发的任何Web应用程序时,我们无法获得任何适当的元素进行交互。 我们必须和很多人一起工作。 因此,在从下拉列表中选择任何值时,我们必须使用sendkeys。 或者简单地键入单个字符,然后从建议列表中选择值。 以上两个解决方案对我来说很有效

    谢谢 -达瓦尔