如何获取Webtable的动态Xpath?

时间:2018-07-04 08:22:44

标签: java html selenium

List<WebElement>table=driver.findElements(By.xpath("//*[@id=\"prodDetails\"]/div[2]/div[1]/div/div[2]/div/div/table/tbody/tr"));
JavascriptExecutor jse = (JavascriptExecutor) driver;
// jse.executeScript("arguments[0].scrollIntoView();",table);
jse.executeScript("arguments[0].style.border='3px solid red'",table);
int row= table.size();

我无法获得所需的行数和列数。我提供的xpath在站点上找不到表

链接:Click here

我必须获取手机的规格。

4 个答案:

答案 0 :(得分:1)

代替此xpath:

//*[@id=\"prodDetails\"]/div[2]/div[1]/div/div[2]/div/div/table/tbody/tr  

使用此:

//*[@id="prodDetails"]/div[2]/div[1]/div/div[2]/div/div/table/tbody/tr  

尽管我不建议您使用绝对xpath。您可以使用相对xpath,它更易读,更容易。

相对Xpath:

//div[@id='prodDetails']/descendant::div[@class='pdTab'][1]/descendant::tbody/tr  

在代码中,例如:

List<WebElement>table=driver.findElements(By.xpath("//div[@id='prodDetails']/descendant::div[@class='pdTab'][1]/descendant::tbody/tr"));

答案 1 :(得分:0)

代替绝对xpath:

//*[@id=\"prodDetails\"]/div[2]/div[1]/div/div[2]/div/div/table/tbody/tr  

我建议使用简单的相对xpath:

//*[@id='prodDetails']//table/tbody/tr  

如果页面中没有其他表,则此xpath将起作用。否则,请确保两个表都可以使用某些属性进行区分

答案 2 :(得分:0)

您可以使用下面的Xpath获取总行数。

在上面的链接中,我们有多个具有相同类的节,并且两个表也具有相似的定位符。因此,您需要根据下表名称获取元素

注意:您可以不使用<child-checkbox [parentFormGroup]="form" [name]="'nameOfCheckbox'" [ngModel]="name"" ngDefaultControl> </child-checkbox>

JavascriptExecutor

输出:  16

假设,如果要获取其他信息表行的详细信息,可以通过将上述部分替换为 WebDriverWait wait=new WebDriverWait(driver,20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='section techD']//span[text()='Technical Details']/ancestor::div[@class='section techD']//table//tr"))); List<WebElement> rowElementList=driver.findElements(By.xpath("//div[@class='section techD']//span[text()='Technical Details']/ancestor::div[@class='section techD']//table//tr")); int row= rowElementList.size(); System.out.println(row);//16

来使用上面的Xpath。
Additional Information

输出: 5

最后,您可以遍历列表并提取表内容详细信息

答案 3 :(得分:0)

XPATH可能很难阅读,特别是当您需要大量使用它时。

您可以尝试univocity-html-parser

    HtmlElement e = HtmlParser.parseTree(new UrlReaderProvider("your_url"));

    List<HtmlElement> rows = e.query()
            .match("div").precededBy("div").withExactText("Technical Details")
            .match("tr").getElements();

    for(HtmlElement row : rows){
        System.out.println(row.text());
    }

以上代码将打印出来:

    OS Android
    RAM 2 GB
    Item Weight 150 g
    Product Dimensions 7.2 x 14.2 x 0.9 cm
    Batteries: 1 AA batteries required. (included)
    Item model number G-550FY
    Wireless communication technologies Bluetooth, WiFi Hotspot
    Connectivity technologies GSM, (850/900/1800/1900 MHz), 4G LTE, (2300/2100/1900/1800/850/900 MHz)
    Special features Dual SIM, GPS, Music Player, Video Player, FM Radio, Accelerometer, Proximity sensor, E-mail
    Other camera features 8MP primary & 5MP front
    Form factor Touchscreen Phone
    Weight 150 Grams
    Colour Gold
    Battery Power Rating 2600
    Whats in the box Handset, Travel Adaptor, USB Cable and User Guide

或者,下面的代码更有用,因为我相信您可能也希望从该页面中获得更多的东西,而通常需要以数据获取行:

    HtmlEntityList entityList = new HtmlEntityList();
    HtmlEntitySettings product = entityList.configureEntity("product");

    PartialPath technicalDetailRows = product.newPath()
            .match("div").precededBy("div").withExactText("Technical Details")
            .match("tr");

    technicalDetailRows.addField("technical_detail_field").matchFirst("td").classes("label").getText();
    technicalDetailRows.addField("technical_detail_value").matchLast("td").classes("value").getText();

    HtmlParserResult results =  new HtmlParser(entityList).parse(new UrlReaderProvider("your_url")).get("product");

    System.out.println("-- " + Arrays.toString(results.getHeaders())  + " --");
    for(String[] row : results.getRows()){
        System.out.println(Arrays.toString(row));
    }

现在会产生:

    OS = Android
    RAM = 2 GB
    Item Weight = 150 g
    Product Dimensions = 7.2 x 14.2 x 0.9 cm
    Batteries: = 1 AA batteries required. (included)
    Item model number = G-550FY
    Wireless communication technologies = Bluetooth, WiFi Hotspot
    Connectivity technologies = GSM, (850/900/1800/1900 MHz), 4G LTE, (2300/2100/1900/1800/850/900 MHz)
    Special features = Dual SIM, GPS, Music Player, Video Player, FM Radio, Accelerometer, Proximity sensor, E-mail
    Other camera features = 8MP primary & 5MP front
    Form factor = Touchscreen Phone
    Weight = 150 Grams
    Colour = Gold
    Battery Power Rating = 2600
    Whats in the box = Handset, Travel Adaptor, USB Cable and User Guide

披露:我是这个图书馆的作者。它是商业上的封闭源代码,但是可以节省很多开发时间。