当前位置: 首页 > 知识库问答 >
问题:

无法选择下拉列表,显示不可见

许嘉珍
2023-03-14
   import java.awt.Toolkit;    
    import org.junit.Assert;
    import org.openqa.selenium.By;
    import org.openqa.selenium.Dimension;
    import org.openqa.selenium.JavascriptExecutor;
    import org.openqa.selenium.Keys;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.WebElement;
    import org.openqa.selenium.chrome.ChromeDriver;
    import org.openqa.selenium.chrome.ChromeOptions;
    import org.openqa.selenium.interactions.Actions;
    import org.openqa.selenium.remote.DesiredCapabilities;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.Select;
    import org.openqa.selenium.support.ui.WebDriverWait;

    public class PracticeTest {
    public static void main(String[] args) throws InterruptedException {
        WebDriver driver;
        WebDriverWait wait;

        System.setProperty("webdriver.chrome.driver", "C:\\Users\\dilu316\\Downloads\\selenium workspace\\chromedriver\\chromedriver.exe");

        //approach 1 - to maximize screen in chrome
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--start-maximized");;
        driver = new ChromeDriver(options);
        wait=new WebDriverWait(driver, 50);

        //if the below method wont work for maximizing screen then above method we can use
        //WebDriver driver = new ChromeDriver();
        //Thread.sleep(5000);
        //driver.manage().window().maximize();

        driver.get("http://ia.ca/");

        //approach 2 - to maximize screen in chrome
        /*Toolkit toolkit = Toolkit.getDefaultToolkit();
        int height = (int)toolkit.getScreenSize().getHeight();
        int width  = (int)toolkit.getScreenSize().getWidth();
        System.out.println(height + "--" + width);

        driver.manage().window().setSize(new Dimension(width, height));*/


        driver.findElement(By.xpath("//*[@id='nav-secondaire']//a[@data-utag-name='loans']")).click();
        driver.findElement(By.xpath("//a[contains(text(),'Mortgages')]")).click();
        //in the upper xpath we can put a ". in place of text()" also

        //method 1 -if element is not clickable
        /*WebDriverWait wait = new WebDriverWait(driver, 100);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(.,'Calculate your payments')]")));*/

        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("scroll(255, 644)");
        driver.findElement(By.xpath("//a[contains(.,'Calculate your payments')]")).click();

        WebElement priceSlideLocator = driver.findElement(By.xpath("//div[@class='slider-handle min-slider-handle custom']"));
        WebElement slideTrack = driver.findElement(By.xpath("//div[@class='slider-track-high']"));
        Dimension sliderSize = slideTrack.getSize();
        int sliderWidth = sliderSize.getWidth();
        int xCoord = priceSlideLocator.getLocation().getX();
        System.out.println(xCoord);
        System.out.println(sliderWidth);
        Thread.sleep(10);
        Actions builder = new Actions(driver);
        builder.moveToElement(priceSlideLocator)
        .click()
        .dragAndDropBy(priceSlideLocator, xCoord+sliderWidth,0)
        .build()
        .perform();

        WebElement hiddenPriceLocator = driver.findElement(By.xpath("//input[@id='sliderPrixPropriete']"));
        int priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
        if(priceValue==2000000){
            System.out.println("price value is 2000000");
        }

        String stylePercent = priceSlideLocator.getAttribute("style");
        if(stylePercent.contains("left: 100%")){
            System.out.println("slide is 100%");
        }

        priceSlideLocator = driver.findElement(By.xpath("//div[@class='slider-handle min-slider-handle custom']"));
        slideTrack = driver.findElement(By.xpath("//div[@class='slider-track-high']"));
        sliderSize = slideTrack.getSize();
        //sliderWidth = sliderSize.getWidth();
        xCoord = priceSlideLocator.getLocation().getX();
        System.out.println(xCoord);
        Thread.sleep(10);
        Actions builder2 = new Actions(driver);
        builder2.moveToElement(priceSlideLocator).click().dragAndDropBy(priceSlideLocator,-(xCoord+sliderWidth),0).build().perform();
        System.out.println("slide BACK%");

        hiddenPriceLocator = driver.findElement(By.xpath("//input[@id='sliderPrixPropriete']"));
        //priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
        WebElement plusButton = driver.findElement(By.id("PrixProprietePlus"));
        for(int i=0; i<2;i++){
            plusButton.click();
            priceValue = Integer.parseInt(hiddenPriceLocator.getAttribute("value"));
            if(priceValue==500000){
                System.out.println("purchase price is 500000");
            }

        }

        WebElement downPlusButton = driver.findElement(By.id("MiseDeFondPlus"));
        downPlusButton.click();
        WebElement downHiddenPrice = driver.findElement(By.id("sliderMiseDeFond"));
        String downPrice=downHiddenPrice.getAttribute("value");
        int downPricevalue = Integer.parseInt(downPrice);
        System.out.println("Down payment is " + downPricevalue);

 code to check the visibility   


    if (driver.findElement(By.xpath("//select[@id='Amortissement']")).isDisplayed()) {
            System.out.println("Element is Visible");
        } else {
            System.out.println("Element is InVisible");
        }

        //To check Element Present


if (driver.findElements(By.xpath("//select[@id='Amortissement']")).size() != 0) {
        System.out.println("Element is Present");
    } else {
        System.out.println("Element is Absent");
    }

    //To check Enable
    if (driver.findElement(By.xpath("//select[@id='Amortissement']")).isEnabled()) {
        System.out.println("Element is Enable");
    } else {
        System.out.println("Element is Disabled");
    }
    /*WebElement drpAmortization = wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//select[@id='Amortissement']")));
    Select dropDownAmortization = new Select(drpAmortization);
    dropDownAmortization.selectByVisibleText("20 years");*/

    WebElement drpAmortization = driver.findElement(By.xpath("//select[@id='Amortissement']"));
    JavascriptExecutor executor = (JavascriptExecutor) driver;
    //executor.executeScript("arguments[0].click();", drpAmortization);
    executor.executeScript("window.document.getElementById('Amortissement').click()");
    Select dropDownAmortization = new Select(drpAmortization);
    dropDownAmortization.selectByVisibleText("20 years");

    }


}

WebElement drpAmentization=driver.findElement(by.xpath(“//select[@id='Amentissement']”));

共有1个答案

端木承业
2023-03-14

我已经检查了页面的html代码,下拉列表不是常规的选择下拉列表。它只是一个按钮之类的东西。所以方法就在下面,

>

  • 单击向下箭头按钮:

    driver.findElement(By.xpath("//*[@id='form_calculateur_versements']/div[4]//b")).click();
    

    现在选项将可见。

    driver.findElement(By.xpath("//*[@id='form_calculateur_versements']/div[4]//li[2]")).click();
    

  •  类似资料:
    • 我有一个用来选择某些元素的代码,当你点击geticon按钮并显示正确的选项值时,该代码工作得很好。问题是我不确定如何显示下拉菜单的选择选项值(而不是按钮)。 这是我的Jsfiddle null null

    • 我试图简单地点击下拉列表,在我输入单词前进后显示。但是我总是被抛出一个错误。线程“main”org.openqa.selenium.NoSuChelementException:没有这样的元素:无法找到元素:

    • 我想在选择另一个select元素的一个选项时显示一个select元素,在选择另一个选项时隐藏它。 这是JavaScript: 感谢任何帮助。谢谢

    • 我想在下拉列表中隐藏所选项目。 我试图从选择事件的数据源中删除该项目,并直接将文本和值分配给下拉列表。但是值将是空的,可能是因为设置的值不存在于数据源中。在剑道留档中找不到解决方案。

    • 我试图从下拉菜单中选择一个选项,然后单击“搜索”,但我无法获得“选择”标签。 我抓取的HTML如下: 我想选择的选项是: 我使用的代码如下: 所以,我试图以不同的方式获得“选择”标签,我得到了不同的问题。 例如: 第一次尝试) 我得到: 第二次尝试) 我得到: 第三次尝试) 我得到一个空列表: 第 4 次尝试) 我得到一个空列表: 第5次尝试) 我得到: 有人知道如何解决这个问题吗?提前感谢!

    • 我正在尝试自动选择硒离子中的下拉列表,但我无法使其正常工作。 基本上,我在菜单项上记录了一次单击,这会显示下拉菜单,但每当我在其中一个选项上使用单击命令时,它都会关闭菜单,而不会选择新选项。我也尝试了select命令,但我一直得到“指定元素不是select” 任何想法?