我想在ul标签下阅读list标签的内容。但我无法使用以下代码阅读。

时间:2017-08-05 08:32:35

标签: java selenium

网址:https://www.amazon.in/s/ref=sr_pg_2?rh=n%3A1571271031%2Cn%3A%211571272031%2Cn%3A1968024031%2Cn%3A1968093031%2Cp_n_pct-off-with-tax%3A70-%2Cp_98%3A10440597031&page=2&bbn=1968093031&ie=UTF8&qid=1501915937&lo=apparel

代码列为Fallows:

try{

    //WebElement e1 = driver.findElement(By.cssSelector("#atfResults > ul"));
    WebElement e1 = driver.findElement(By.id("s-results-list-atf"));
    System.out.println(e1);

    List<WebElement> links = e1.findElements(By.cssSelector("#atfResults > ul"));;
    for (int i = 1; i < links.size(); i++)
    {
        System.out.println(links.get(i).getText());
    }
    }
    catch(Exception e){
        System.out.println(e.getMessage());
    }
}

如果有人得到解决方案,请告诉我。

1 个答案:

答案 0 :(得分:0)

要阅读url&#34; https://www.amazon.in/s/ref=sr_pg_2?rh=n%3A1571271031%2Cn%3A%211571272031%2Cn%3A1968024031%2Cn%3A1968093031%2Cp_n_pct-off-with-tax%3A70-%2Cp_98%3A10440597031&page=2&bbn=1968093031&ie=UTF8&qid=1501915937&lo=apparel&#34;的ul标记下的列表标记(项目名称)的内容,您可以使用以下代码块:

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;

public class Q45519900_amazon_list 
{

    public static void main(String[] args) 
    {
        System.setProperty("webdriver.chrome.driver", "C:\\Utility\\BrowserDrivers\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("start-maximized");
        options.addArguments("disable-infobars");
        options.addArguments("--disable-extensions"); 
        WebDriver driver = new ChromeDriver(options);
        driver.navigate().to("https://www.amazon.in/s/ref=sr_pg_2?rh=n%3A1571271031%2Cn%3A%211571272031%2Cn%3A1968024031%2Cn%3A1968093031%2Cp_n_pct-off-with-tax%3A70-%2Cp_98%3A10440597031&page=2&bbn=1968093031&ie=UTF8&qid=1501915937&lo=apparel");
        List<WebElement> element = driver.findElements(By.xpath("//ul[@id='s-results-list-atf']/li[starts-with(@id, 'result_')]/div/div[@class='a-row a-spacing-none']/div/a/h2"));
        System.out.println("Total items : "+element.size());
        for (WebElement my_element:element)
        {
        String title = my_element.getAttribute("innerHTML");
        System.out.println("Item name is : "+title);
        }

    }

}

控制台上的输出是:

Total items : 48
Item name is : GlobalRang Men's Cotton Formal Shirt
Item name is : Rapphael Men's Full sleeve Casual Shirt
Item name is : Feed Up Pack of 4 Men's Pocket Shirts
Item name is : Rapphael Men's Casual Shirt
Item name is : Arihant Men's Solid Formal Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Mossimo Men's Casual Shirt
Item name is : Urban Nomad Orange Cotton Formal Shirt
Item name is : Men's Cotton Shirt
Item name is : Slub Mens Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Rapphael Men's Casual Shirt
Item name is : Arihant Men's Full Sleeves Regular fit Formal Shirt
Item name is : Arihant Men's Formal Shirt
Item name is : Rapphael Charcoal Grey &amp; Blue color Casual Shirt for men
Item name is : Urban Nomad Yellow Cotton Formal Shirt
Item name is : GlobalRang Men's Cotton Casual Royal Blue Stand Collar Shirt
Item name is : SHADE 45 Men's Premium designed Cotton Full Sleeve Slim fit Orange color Plain…
Item name is : Basics Men's Casual Shirt
Item name is : People Men's Casual Shirt
Item name is : Dennison Men's Casual Shirt
Item name is : IZOD Men's Casual Shirt
Item name is : Deezeno Men's Cotton Shirt
Item name is : Easies Men's Casual Shirt
Item name is : Dennison Men's Formal Shirt
Item name is : Slub Men's Casual Shirt
Item name is : Scott International Men's Premium Cotton Blend Pullover Hoodie Sweatshirt With Zip…
Item name is : Rapphael Men's Full sleeve Casual Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Dennison Men's Casual Shirt
Item name is : Arihant Men's Full Sleeves Regular Fit Striped Poly Cotton Formal Shirt
Item name is : GHPC Men's 100%Cotton Slim Fit Full Sleeves Printed Casual Shirt
Item name is : Basics Men's Casual Shirt
Item name is : Rapphael Green &amp; Black Color Casual Shirt for men
Item name is : Urban Nomad Yellow Cotton Formal Shirt
Item name is : Arihant Men's Formal Shirt
Item name is : Trendy Trotters Pink Fullsleeves Shirt
Item name is : Arihant Men's Full Sleeves Regular fit Formal Shirt
Item name is : Dennison Men's Formal Shirt
Item name is : IZOD Men's Casual Shirt
Item name is : Dennison Men's Casual Shirt
Item name is : GHPC Men's 100%Cotton Slim Fit Full Sleeves Striped Casual Shirt
Item name is : Stylox Bull Printed White Shirt [007
Item name is : Deezeno Men's Cotton Shirt
Item name is : Dennison Men's Formal Shirt
Item name is : People Men's Casual Shirt
Item name is : United Colors of Benetton Men's Casual Shirt
Item name is : Rapphael Orange color Casual Shirt for men

如果这回答你的问题,请告诉我。