Class AbstractWebDriver
-
- All Implemented Interfaces:
-
ai.platon.pulsar.crawl.fetch.driver.JvmWebDriver,ai.platon.pulsar.crawl.fetch.driver.WebDriver,java.io.Closeable,java.lang.AutoCloseable,kotlin.Comparable
public abstract class AbstractWebDriver extends AbstractJvmWebDriver implements Comparable<AbstractWebDriver>, WebDriver, JvmWebDriver
-
-
Field Summary
-
Constructor Summary
Constructors Constructor Description AbstractWebDriver(Browser browser, Integer id)
-
Method Summary
Modifier and Type Method Description DurationgetIdleTimeout()The idle timeout. UnitsetIdleTimeout(Duration idleTimeout)The idle timeout. DurationgetWaitForElementTimeout()The timeout to wait for an element to appear. UnitsetWaitForElementTimeout(Duration waitForElementTimeout)The timeout to wait for an element to appear. StringgetName()The driver name. Function1<String, Long>getDelayPolicy()NavigateEntrygetNavigateEntry()The current navigation entry. UnitsetNavigateEntry(NavigateEntry navigateEntry)The current navigation entry. NavigateHistorygetNavigateHistory()The navigation history. BooleangetSupportJavascript()Whether the driver supports javascript. BooleanisMockedPageSource()Whether the page source is mocked. BooleanisRecovered()Whether the driver is recovered from the browser's tab list. UnitsetRecovered(Boolean isRecovered)Whether the driver is recovered from the browser's tab list. BooleanisReused()Whether the driver is recovered and is reused to serve new tasks. UnitsetReused(Boolean isReused)Whether the driver is recovered and is reused to serve new tasks. AtomicReference<WebDriver.State>getState()The state of the driver. Map<String, Object>getData()The associated data. InstantgetLastActiveTime()The time of the last active action. UnitsetLastActiveTime(Instant lastActiveTime)The time of the last active action. BooleanisInit()Whether the driver is initialized. BooleanisReady()Whether the driver is ready to work. BooleanisWorking()Whether the driver is working. BooleanisRetired()Whether the driver is retired and should be quit as soon as possible. BooleanisQuit()Whether the driver is quit. BooleanisCanceled()Whether the current driver task is canceled and should return as soon as possible. BooleanisCrashed()Whether the driver is crashed. Map<String, Object>getMainRequestHeaders()The main request's http headers. List<Map<String, String>>getMainRequestCookies()The main request's http cookies. IntegergetMainResponseStatus()The main response's status. StringgetMainResponseStatusText()The main response's status text. Map<String, Object>getMainResponseHeaders()The main response's http headers. BrowsergetBrowser()The browser of the driver. IntegergetId()The driver id. Unitfree()Mark the driver as free, so it can be used to fetch a new page. UnitstartWork()Mark the driver as working, so it can not be used to fetch another page. Unitretire()Mark the driver as retired, so it can not be used to fetch any page, and should be quit as soon as possible. Unitcancel()Mark the driver as canceled, so the fetch process should return as soon as possible, and the fetch result should be dropped. JvmWebDriverjvm()Returns a JvmWebDriver to support other JVM languages, such as java, clojure, scala, and so on, the other JVM languages might have difficulty to handle kotlin suspend methods. UnitnavigateTo(String url)Returns the main resource response. Stringlocation()Returns the document's location evaluated by javascript. StringbaseURI()Returns the document's baseURI evaluated by javascript. LongwaitForSelector(String selector, Long timeoutMillis)Returns when element specified by selector satisfies {@code state} option. LongwaitForSelector(String selector)Returns when element specified by selector satisfies {@code state} option. LongwaitForNavigation()LongwaitForNavigation(Long timeoutMillis)ObjectevaluateSilently(String expression)Executes JavaScript in the context of the currently selected frame or window. BooleanisVisible(String selector)Returns whether the element is visible. BooleanisChecked(String selector)Returns whether the element is checked. UnitscrollDown(Integer count)The current page frame scrolls down for count times. UnitscrollUp(Integer count)The current page frame scrolls up for count times. UnitscrollToTop()The current page frame scrolls to the top. UnitscrollToBottom()The current page frame scrolls to the bottom. UnitscrollToMiddle(Float ratio)The current page frame scrolls to the middle. StringclickNthAnchor(Integer n, String rootSelector)StringouterHTML()Returns the document's HTML markup. StringouterHTML(String selector)Returns the node's HTML markup, the node is located by selector. StringselectFirstTextOrNull(String selector)Returns the node's text content, the node is located by selector. List<String>selectTexts(String selector)Returns the nodes' text contents. StringselectFirstAttributeOrNull(String selector, String attrName)Returns the node's attribute value, the node is located by selector, the attribute is attrName. List<String>selectAttributes(String selector, String attrName)Returns the nodes' attribute values, the nodes are located by selector, the attribute is attrName. UnitclickTextMatches(String selector, String pattern, Integer count)This method clicks an element with selector whose text content matches pattern, and then focuses it. UnitclickMatches(String selector, String attrName, String pattern, Integer count)Unitcheck(String selector)This method check an element with selector. Unituncheck(String selector)This method uncheck an element with selector. ConnectionnewJsoupSession()Create a new Jsoup session with the last page's context, which means, the same headers and cookies. Connection.ResponseloadJsoupResource(String url)Load the url as a resource with Jsoup rather than browser rendering, with the last page's context, which means, the same headers and cookies. NetworkResourceResponseloadResource(String url)Load url as a resource without browser rendering, with the last page's context, which means, the same headers and cookies. Booleanequals(Object other)IntegerhashCode()IntegercompareTo(AbstractWebDriver other)StringtoString()-
Methods inherited from class ai.platon.pulsar.crawl.fetch.driver.WebDriver
addBlockedURLs, addInitScript, addProbabilityBlockedURLs, allTexts, awaitTermination, boundingBox, bringToFront, captureScreenshot, captureScreenshot, clearBrowserCookies, click, clickMatches, clickablePoint, currentUrl, deleteCookies, deleteCookies, dragAndDrop, evaluate, evaluateDetail, exists, firstText, focus, getBrowserType, getCookies, isHidden, isIdle, mouseWheelDown, mouseWheelUp, moveMouseTo, moveMouseTo, navigateTo, pageSource, pause, scrollTo, setTimeouts, stop, terminate, type, visible, waitForNavigation, waitForPage, waitForSelector -
Methods inherited from class ai.platon.pulsar.crawl.fetch.driver.AbstractJvmWebDriver
addBlockedURLsAsync, addInitScriptAsync, boundingBoxAsync, bringToFrontAsync, captureScreenshotAsync, captureScreenshotAsync, checkAsync, clickAsync, clickMatchesAsync, clickMatchesAsync, clickNthAnchorAsync, clickablePointAsync, currentUrlAsync, dragAndDropAsync, evaluateAsync, evaluateSilentlyAsync, existsAsync, getCookiesAsync, isCheckedAsync, isHiddenAsync, isVisibleAsync, loadResourceAsync, mainRequestCookiesAsync, mainRequestHeadersAsync, mouseWheelDownAsync, mouseWheelUpAsync, moveMouseToAsync, navigateToAsync, navigateToAsync, newJsoupSessionAsync, outerHTMLAsync, pageSourceAsync, pauseAsync, scrollDownAsync, scrollToAsync, scrollToBottomAsync, scrollToMiddleAsync, scrollToTopAsync, scrollUpAsync, selectAttributesAsync, selectFirstAttributeOptionalAsync, selectFirstAttributeOrNullAsync, selectFirstTextOptionalAsync, selectFirstTextOrNullAsync, selectTextsAsync, setTimeoutsAsync, stopAsync, terminateAsync, typeAsync, uncheckAsync, visibleAsync, waitForNavigationAsync, waitForNavigationAsync, waitForNavigationAsync, waitForSelectorAsync, waitForSelectorAsync, waitForSelectorAsync -
Methods inherited from class ai.platon.pulsar.crawl.fetch.driver.JvmWebDriver
clickAsync, clickMatchesAsync, clickMatchesAsync -
Methods inherited from class java.io.Closeable
close -
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
-
Method Detail
-
getIdleTimeout
Duration getIdleTimeout()
The idle timeout.
-
setIdleTimeout
Unit setIdleTimeout(Duration idleTimeout)
The idle timeout.
-
getWaitForElementTimeout
Duration getWaitForElementTimeout()
The timeout to wait for an element to appear.
-
setWaitForElementTimeout
Unit setWaitForElementTimeout(Duration waitForElementTimeout)
The timeout to wait for an element to appear.
-
getDelayPolicy
Function1<String, Long> getDelayPolicy()
-
getNavigateEntry
NavigateEntry getNavigateEntry()
The current navigation entry.
-
setNavigateEntry
Unit setNavigateEntry(NavigateEntry navigateEntry)
The current navigation entry.
-
getNavigateHistory
NavigateHistory getNavigateHistory()
The navigation history.
-
getSupportJavascript
Boolean getSupportJavascript()
Whether the driver supports javascript. Web drivers such as MockDriver do not support javascript.
-
isMockedPageSource
Boolean isMockedPageSource()
Whether the page source is mocked.
-
isRecovered
Boolean isRecovered()
Whether the driver is recovered from the browser's tab list.
-
setRecovered
Unit setRecovered(Boolean isRecovered)
Whether the driver is recovered from the browser's tab list.
-
setReused
Unit setReused(Boolean isReused)
Whether the driver is recovered and is reused to serve new tasks.
-
getState
AtomicReference<WebDriver.State> getState()
The state of the driver.
State.INIT: The driver is initialized.
State.READY: The driver is ready to work.
State.WORKING: The driver is working.
State.RETIRED: The driver is retired and should be quit as soon as possible.
State.QUIT: The driver is quit.
-
getLastActiveTime
Instant getLastActiveTime()
The time of the last active action.
-
setLastActiveTime
Unit setLastActiveTime(Instant lastActiveTime)
The time of the last active action.
-
isCanceled
Boolean isCanceled()
Whether the current driver task is canceled and should return as soon as possible.
-
getMainRequestHeaders
Map<String, Object> getMainRequestHeaders()
The main request's http headers.
-
getMainRequestCookies
List<Map<String, String>> getMainRequestCookies()
The main request's http cookies.
-
getMainResponseStatus
Integer getMainResponseStatus()
The main response's status.
-
getMainResponseStatusText
String getMainResponseStatusText()
The main response's status text.
-
getMainResponseHeaders
Map<String, Object> getMainResponseHeaders()
The main response's http headers.
-
getBrowser
Browser getBrowser()
The browser of the driver. The browser defines methods and events to manipulate a real browser.
-
retire
Unit retire()
Mark the driver as retired, so it can not be used to fetch any page, and should be quit as soon as possible.
-
cancel
Unit cancel()
Mark the driver as canceled, so the fetch process should return as soon as possible, and the fetch result should be dropped.
-
jvm
JvmWebDriver jvm()
Returns a JvmWebDriver to support other JVM languages, such as java, clojure, scala, and so on, the other JVM languages might have difficulty to handle kotlin suspend methods.
-
navigateTo
Unit navigateTo(String url)
Returns the main resource response. In case of multiple redirects, the navigation will resolve with the first non-redirect response.
- Parameters:
url- URL to navigate page to.
-
location
String location()
Returns the document's location evaluated by javascript.
In javascript, the
window.location, ordocument.location, is a read-only property returns a Location object, which contains information about the URL of the document and provides methods for changing that URL and loading another URL.To retrieve just the URL as a string, the read-only
document.URLproperty can also be used.
-
baseURI
String baseURI()
Returns the document's baseURI evaluated by javascript.
In javascript, the baseURI is a property of Node, it's the absolute base URL of the document containing the node. A baseURI is used to resolve relative URLs.
The base URL is determined as follows:
By default, the base URL is the location of the document (as determined by window.location).
If the document has an
<base>element, its href attribute is used.
-
waitForSelector
Long waitForSelector(String selector, Long timeoutMillis)
Returns when element specified by selector satisfies {@code state} option. Returns the time remaining until timeout.
-
waitForSelector
Long waitForSelector(String selector)
Returns when element specified by selector satisfies {@code state} option.
-
waitForNavigation
Long waitForNavigation()
-
waitForNavigation
Long waitForNavigation(Long timeoutMillis)
-
evaluateSilently
Object evaluateSilently(String expression)
Executes JavaScript in the context of the currently selected frame or window. The script fragment provided will be executed as the body of an anonymous function.
All possible exceptions are suppressed and do not throw.
- Parameters:
expression- Javascript expression to evaluate- Returns:
Remote object value in case of primitive values or JSON values (if it was requested).
-
scrollDown
Unit scrollDown(Integer count)
The current page frame scrolls down for count times.
- Parameters:
count- The times to scroll down.
-
scrollUp
Unit scrollUp(Integer count)
The current page frame scrolls up for count times.
- Parameters:
count- The times to scroll up.
-
scrollToTop
Unit scrollToTop()
The current page frame scrolls to the top.
-
scrollToBottom
Unit scrollToBottom()
The current page frame scrolls to the bottom.
-
scrollToMiddle
Unit scrollToMiddle(Float ratio)
The current page frame scrolls to the middle.
- Parameters:
ratio- The ratio of the page to scroll to, 0.0 means the top, 1.0 means the bottom.
-
clickNthAnchor
String clickNthAnchor(Integer n, String rootSelector)
-
outerHTML
String outerHTML()
Returns the document's HTML markup.
If the document does not exist, returns null.
- Returns:
The HTML markup of the document.
-
outerHTML
String outerHTML(String selector)
Returns the node's HTML markup, the node is located by selector.
If the node does not exist, returns null.
- Parameters:
selector- The selector to locate the node.- Returns:
The HTML markup of the node.
-
selectFirstTextOrNull
String selectFirstTextOrNull(String selector)
Returns the node's text content, the node is located by selector.
If the node does not exist, returns null.
- Parameters:
selector- The selector to locate the node.- Returns:
The text content of the node.
-
selectTexts
List<String> selectTexts(String selector)
Returns the nodes' text contents.
If the nodes do not exist, returns an empty list.
- Parameters:
selector- The selector to locate the nodes.- Returns:
The text contents of the nodes.
-
selectFirstAttributeOrNull
String selectFirstAttributeOrNull(String selector, String attrName)
Returns the node's attribute value, the node is located by selector, the attribute is attrName.
If the node does not exist, or the attribute does not exist, returns null.
- Parameters:
selector- The selector to locate the node.attrName- The attribute name to retrieve.- Returns:
The attribute value of the node.
-
selectAttributes
List<String> selectAttributes(String selector, String attrName)
Returns the nodes' attribute values, the nodes are located by selector, the attribute is attrName.
If the nodes do not exist, or the attribute does not exist, returns an empty list.
- Parameters:
selector- The selector to locate the nodes.attrName- The attribute name to retrieve.- Returns:
The attribute values of the nodes.
-
clickTextMatches
Unit clickTextMatches(String selector, String pattern, Integer count)
This method clicks an element with selector whose text content matches pattern, and then focuses it. If there's no element matching selector, or the element's text content doesn't match pattern, nothing to do.
- Parameters:
selector-A {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors | selector } of an element to focus. If there are multiple elements satisfying the selector, the first will be focused.
-
check
Unit check(String selector)
- Parameters:
selector-A {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors | selector } of an element to check. If there are multiple elements satisfying the selector, the first will be focused.
-
uncheck
Unit uncheck(String selector)
This method uncheck an element with selector. If there's no element matching selector, nothing to do.
- Parameters:
selector-A {@link https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Selectors | selector } of an element to uncheck. If there are multiple elements satisfying the selector, the first will be focused.
-
newJsoupSession
Connection newJsoupSession()
Create a new Jsoup session with the last page's context, which means, the same headers and cookies.
-
loadJsoupResource
Connection.Response loadJsoupResource(String url)
Load the url as a resource with Jsoup rather than browser rendering, with the last page's context, which means, the same headers and cookies.
-
loadResource
NetworkResourceResponse loadResource(String url)
Load url as a resource without browser rendering, with the last page's context, which means, the same headers and cookies.
-
compareTo
Integer compareTo(AbstractWebDriver other)
-
-
-
-