Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 列表<;WebElement>;给出空白结果_Java_Arraylist_Selenium Webdriver - Fatal编程技术网

Java 列表<;WebElement>;给出空白结果

Java 列表<;WebElement>;给出空白结果,java,arraylist,selenium-webdriver,Java,Arraylist,Selenium Webdriver,我正在为一些网站创建一个自动测试,我正在使用用Java编写的WebDriver和代码。我在这里面临两个问题 click()方法不起作用 当我试图阅读WebElement列表时,它显示为空白。但它显示的是正确的size() 我的代码: System.setProperty("webdriver.gecko.driver", "D:\\back up\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe"); WebDriver driver=new Fir

我正在为一些网站创建一个自动测试,我正在使用用Java编写的WebDriver和代码。我在这里面临两个问题

  • click()
    方法不起作用
  • 当我试图阅读WebElement列表时,它显示为空白。但它显示的是正确的
    size()
  • 我的代码:

    System.setProperty("webdriver.gecko.driver", "D:\\back up\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe");
    WebDriver driver=new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
    driver.get("http://www.americangolf.co.uk/golf-clubs/fairway-woods");
    
    List<WebElement> allBrands = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul/li/a"));
    List<WebElement> allBrandsInShowAll = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul/li/a"));
    
    System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
    driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click(); // This is not clicking on ShowAll Button
    for(WebElement ele :allBrandsInShowAll) {
      System.out.println("Name=>"+ele.getText());
    }
    
    for(WebElement ele :allBrands) {
     System.out.println("Name=>"+ele.getText()); // Not printing the names of Brands
    }
    
    System.setProperty(“webdriver.gecko.driver”,“D:\\backup\\Proj\\selenium\\Jars\\Drivers\\geckodriver.exe”);
    WebDriver=newfirefoxdriver();
    driver.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
    驱动程序。获取(“http://www.americangolf.co.uk/golf-clubs/fairway-woods");
    列出allBrands=driver.findElements(By.xpath(“/*[@id='secondary']]/div[1]/div[2]/div/ul/li/a”);
    列出allBrandsInShowAll=driver.findElements(By.xpath(“/*[@id='secondary']]/div[1]/div[2]/div/div[2]/ul/li/a”);
    System.out.println(“品牌总数”+(allBrands.size()+allBrandsInShowAll.size());
    driver.findelelement(By.xpath(“//*[@id='secondary']]/div[1]/div[2]/div/div/div[1]/a”)。单击()这不是单击ShowAll按钮
    for(WebElement ele:allBrandsInShowAll){
    System.out.println(“Name=>”+ele.getText());
    }
    对于(WebElement ele:allBrands){
    System.out.println(“Name=>”+ele.getText());//不打印品牌名称
    }
    
  • 您只需使用css选择器
    div.showall
    从品牌选项中单击showall按钮即可

    driver.findElement(By.cssSelector("div.showall")).click();
    
  • 甚至这个xpath也可以直接工作

       driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click()
    
  • 用于获取
    getText()
    的父元素不正确

    List<WebElement> allBrands = driver.findElements(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul/li/a"));
    
    将输出打印为

    Name=>CobraGolf (14)

    Name=>CallawayGolf (10)

    Name=>TaylorMade (八)


    编辑:OP提到,单击
    ShowAll
    按钮后,他无法获得其他品牌选项。原因是它们位于另一个子系统中

    请将您的最终代码修改为此-

        //clicks on ShowAll button
        driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click();
        Thread.sleep(5000);
    
         //gets parent for Categories
         WebElement parentElement = driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul"));
    
        //gets parent for Categories shown after clicking ShowAll
        WebElement secondParentElement = driver.findElement(By.xpath(".//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul"));
    
    
        List<WebElement> allBrands = parentElement.findElements(By.cssSelector("li >a"));
        List<WebElement> allBrandsInShowAll =secondParentElement.findElements(By.cssSelector("li >a"));
    
        System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
    
        for(WebElement ele :allBrands) {
         System.out.println("Name=>"+ele.getText()); 
        }
    
        for(WebElement ele :allBrandsInShowAll) {
             System.out.println("Name=>"+ele.getText()); 
            }
    
        driver.quit();
    
       }
    
        }
    
    //单击ShowAll按钮
    findElement(By.xpath(“//*[@id='secondary']/div[1]/div[2]/div/div[1]/a”)。单击();
    睡眠(5000);
    //获取类别的父级
    WebElement parentElement=driver.findElement(By.xpath(“/*[@id='secondary']]/div[1]/div[2]/div/ul”);
    //获取单击ShowAll后显示的类别的父级
    WebElement secondParentElement=driver.findElement(By.xpath(“./*[@id='secondary']]/div[1]/div[2]/div/div[2]/ul”);
    列出allBrands=parentElement.findElements(By.cssSelector(“li>a”));
    List allBrandsInShowAll=secondParentElement.findElements(By.cssSelector(“li>a”));
    System.out.println(“品牌总数”+(allBrands.size()+allBrandsInShowAll.size());
    对于(WebElement ele:allBrands){
    System.out.println(“Name=>”+ele.getText());
    }
    for(WebElement ele:allBrandsInShowAll){
    System.out.println(“Name=>”+ele.getText());
    }
    driver.quit();
    }
    }
    
    这将打印品牌内部的所有类别-

    品牌总数13

    • Name=>CobraGolf (14)
    • Name=>CallawayGolf (10)
    • Name=>TaylorMade (八)
    • Name=>Ping (六)
    • Name=>Benross (四)
    • Name=>NikeGolf (二)
    • Name=>Titleist (二)
    • Name=>Wilson (二)
    • Name=>WilsonStaff (二)
    • Name=>Fazer (一)
    • Name=>MizunoGolf (一)
    • Name=>USKidsGolf (一)
    • Name=>WilsonDeepRed (一)

    您在尝试获取元素的位置验证了xpath吗?xpath可能是错误的。您在allBrandsInShowAll中试图得到什么?xpath是正确的。我验证了它。请检查此URL“allBrandsInShowAll应包含[+Show All]按钮单击后出现的值。[li值在不同的div下]您应该对所有品牌使用此xpath://*[@id=“secondary”]/div[1]/div[2]/div/ul/li,它将返回size=10。但品牌下的实际值列表为'13',谢谢您的帮助。如果我使用这里提到的CSS选择器。。我得到了“ElementNotInteractiableException”,xpath的“ShowAll”按钮没有被点击。。我需要显示所有按钮内部的值。。它们是-->MizunoGolf(1)USKidsGolf(1)WilsonDeepRed(1)我能够从显示的UI端口获取值,您也可以使用xpath。我也添加了这个。是的,谢谢demouser123,但是我应该能够检索到inside Show All按钮的值……但是没有运气:(到目前为止..非常感谢您的帮助demouser123。
        //clicks on ShowAll button
        driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/div[1]/a")).click();
        Thread.sleep(5000);
    
         //gets parent for Categories
         WebElement parentElement = driver.findElement(By.xpath("//*[@id='secondary']/div[1]/div[2]/div/ul"));
    
        //gets parent for Categories shown after clicking ShowAll
        WebElement secondParentElement = driver.findElement(By.xpath(".//*[@id='secondary']/div[1]/div[2]/div/div[2]/ul"));
    
    
        List<WebElement> allBrands = parentElement.findElements(By.cssSelector("li >a"));
        List<WebElement> allBrandsInShowAll =secondParentElement.findElements(By.cssSelector("li >a"));
    
        System.out.println("Total No of Brands "+(allBrands.size() + allBrandsInShowAll.size()));
    
        for(WebElement ele :allBrands) {
         System.out.println("Name=>"+ele.getText()); 
        }
    
        for(WebElement ele :allBrandsInShowAll) {
             System.out.println("Name=>"+ele.getText()); 
            }
    
        driver.quit();
    
       }
    
        }