本文整理匯總了Java中com.gargoylesoftware.htmlunit.html.HtmlPage.getByXPath方法的典型用法代碼示例。如果您正苦於以下問題:Java HtmlPage.getByXPath方法的具體用法?Java HtmlPage.getByXPath怎麽用?Java HtmlPage.getByXPath使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.gargoylesoftware.htmlunit.html.HtmlPage
的用法示例。
在下文中一共展示了HtmlPage.getByXPath方法的8個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: searchDuck
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public static List<String> searchDuck (String keyword) {
List<String> searchResults = new ArrayList<>();
try{
WebClient webClient = new WebClient(BrowserVersion.CHROME);
HtmlPage page = webClient.getPage("https://duckduckgo.com/html/?q=" + keyword);
List<HtmlAnchor> l = page.getByXPath("//a[@class='result__url']");
for(HtmlAnchor a: l) {
searchResults.add(a.getHrefAttribute());
}
}
catch(Exception e){
System.err.println(e);
}
return searchResults;
}
示例2: shouldShowPetIndexPage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
@Test
public void shouldShowPetIndexPage() throws Exception {
HtmlPage ownerIndexPage = getPage("/pets");
assertEquals(13.0, ownerIndexPage.getByXPath("count(//div[@class='card'])").get(0));
String content = ownerIndexPage.asText();
assertTrue(content.contains("Leo"));
assertTrue(content.contains("Owned by: George Franklin"));
assertTrue(content.contains("Birthday: 2010-09-07"));
assertTrue(content.contains("Type: cat"));
List viewEditDeleteLinks = ownerIndexPage.getByXPath("//div[@class='card'][1]//@href");
assertEquals("/owners/1", ((DomAttr) viewEditDeleteLinks.get(0)).getValue());
assertEquals("/pets/1", ((DomAttr) viewEditDeleteLinks.get(1)).getValue());
assertEquals("/pets/1/edit", ((DomAttr) viewEditDeleteLinks.get(2)).getValue());
assertEquals("/pets/1/delete", ((DomAttr) viewEditDeleteLinks.get(3)).getValue());
}
示例3: shouldShowOwnerIndexPage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
@Test
public void shouldShowOwnerIndexPage() throws Exception {
HtmlPage ownerIndexPage = getPage("/owners");
assertEquals(10.0, ownerIndexPage.getByXPath("count(//tbody/tr)").get(0));
List ownerProperties = ownerIndexPage.getByXPath("//tbody/tr[1]/td/text()");
assertEquals("George", ownerProperties.get(0).toString());
assertEquals("Franklin", ownerProperties.get(1).toString());
assertEquals("110 W. Liberty St.", ownerProperties.get(2).toString());
assertEquals("Madison", ownerProperties.get(3).toString());
assertEquals("6085551023", ownerProperties.get(4).toString());
}
示例4: searchInBaidu
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public void searchInBaidu() throws Exception {
HtmlPage page = webClient.getPage("https://www.baidu.com/");
HtmlForm form = page.getFormByName("f");
HtmlTextInput input = form.getInputByName("wd");
HtmlSubmitInput button = form.getInputByValue("百度一下");
input.setValueAttribute("無錫");
HtmlPage nextPage = button.click();
//System.out.println(nextPage.asXml());
// hit next page
HtmlAnchor next = null;
List list = nextPage.getByXPath("//a");
for(Object obj : list) {
if(obj instanceof HtmlAnchor) {
HtmlAnchor ha = (HtmlAnchor)obj;
//System.out.println(ha.getTextContent());
if(ha.getTextContent().indexOf("百度百科") != -1) {
next = ha;
break;
}
}
}
System.out.println(next.asXml());
System.out.println("--------------------------");
HtmlPage p = next.click();
System.out.println(p.asXml());
}
示例5: shouldOwnerIndexPageNavigateToOwnerDetailPage
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
@Test
public void shouldOwnerIndexPageNavigateToOwnerDetailPage() throws Exception {
HtmlPage ownerIndexPage = getPage("/owners");
List viewEditDeleteButtons = ownerIndexPage.getByXPath("//tbody/tr[1]/td[6]//a");
HtmlAnchor aHref = (HtmlAnchor) viewEditDeleteButtons.get(0);
HtmlPage viewOwnerPage = aHref.click();
assertTrue(viewOwnerPage.getUrl().toString().matches(".*/owners/1$"));
}
示例6: fetchLabelsWithAnyTitle
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
private static List<HtmlLabel> fetchLabelsWithAnyTitle(HtmlPage page) {
return (List<HtmlLabel>) page.getByXPath(LABEL_XPATH);
}
示例7: fetchResourceIDs
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
protected static List<String> fetchResourceIDs(HtmlPage coursePage, final String LIST_ITEMS_XPATH) {
List<String> courseIDs = new ArrayList<>();
List<?> courseListItems = coursePage.getByXPath(LIST_ITEMS_XPATH);
getIDs(courseListItems).forEach(id -> courseIDs.add(id));
return courseIDs;
}
示例8: obtainPersonas
import com.gargoylesoftware.htmlunit.html.HtmlPage; //導入方法依賴的package包/類
public Persona obtainPersonas(String host)
throws FailingHttpStatusCodeException, MalformedURLException,
IOException {
if (this.patterns == null
|| (this.patterns != null && this.patterns.isEmpty()))
initPatterns();
WebClient webClient = new WebClient();
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setThrowExceptionOnFailingStatusCode(false);
webClient.getOptions().setThrowExceptionOnScriptError(false);
HtmlPage htmlPage = null;
Persona persona = new Persona();
persona.setHostPatternKey(host);
;
persona.setPageId(page.toURI().toString());
try {
htmlPage = webClient.getPage(page.toURL());
} catch (Exception e) {
e.printStackTrace(System.out);
webClient.close();
return persona;
}
String pattern = patterns.get(host);
boolean isAnchor = false;
if (pattern.contains("@href")) {
isAnchor = true;
}
List<?> elements = htmlPage.getByXPath(patterns.get(host));
for (int i = 0; i < elements.size(); i++) {
String username = null;
if (isAnchor) {
String link = ((HtmlAnchor) elements.get(i)).getHrefAttribute();
if (isUserLink(link)) {
int index = link.lastIndexOf('/');
username = link.substring(index + 1);
}
} else {
if (elements.get(i) instanceof String) {
username = ((String) elements.get(i)).trim();
} else {
username = ((DomNode) elements.get(i)).asText();
}
}
if (username != null && !username.equals("")) {
persona.getUsernames().add(username);
}
}
webClient.close();
return persona;
}