簡體   English   中英

Jsoup無法解析查詢

[英]Jsoup Could not parse query

我將xpath轉換為Jsoup,下面是我的xpath(在我的硒webdriver中使用)

String number = driver.findElement(By.xpath("//span[@data-dojo-attach-point='subNumber']")).getText();

等效的jsoup

String number =  doc.select(" >span >data-dojo-attach-point=subNumber").text();
        System.out.println(number); 

執行低於錯誤時

Could not parse query 'data-dojo-attach-point=subNumber': unexpected token at '=subNumber'

HTML:

<div class="subHeaders">
            <div class="subHeaderItem">
                <h5 class="smallGray">Number</h5>
                <span data-dojo-attach-point="subNumber">94607506</span>
            </div>
</div>

誰能幫忙。

這是您可以使用selectFirst(String cssQuery)然后是html()檢索數據的方法:

測試類:

import java.io.IOException;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class TestA {

    public static void main(String[] args) throws IOException {


        //this is where chromedriver.exe should be 
        String driverPath = "yourDriverPath";

        System.setProperty("webdriver.chrome.driver", driverPath); 

        WebDriver driver = new ChromeDriver(); ; 

        driver.get("YourURL"); 

        WebDriverWait wait = new WebDriverWait(driver, 15);

        String cssSelector = "span[data-dojo-attach-point=subNumber]";

        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(cssSelector)));

        Document doc = Jsoup.connect("YourURL").get();

        Element subNumber = doc.selectFirst(cssSelector);

        System.out.println(subNumber.html());
    }
}

輸出:

94607506

注意:我已經在筆記本電腦上嘗試了以上操作,並且可以正常工作。

使用此CSS選擇器。

  div.subHeaders > div.subHeaderItem > span

  String number =  doc.select("div.subHeaders > div.subHeaderItem > span").text();

如果頁面已加載,則您將獲得文本。 使用“嘗試Jsoup”來驗證是否能夠獲取文本。

點擊此鏈接 單擊“獲取URL”,然后輸入您要解析的頁面的URL,然后單擊“獲取”。 讓我知道您是否能夠獲得價值。

如果您不介意在此處發布URL,請在此處發布URL。 我們會為您服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM