Skip to content

Commit

Permalink
fix: Page.waitForSelector 查询 aria selector 的逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
fanyong920 committed Dec 18, 2024
1 parent ed315c3 commit 4ace9f0
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/ruiyun/jvppeteer/api/core/Frame.java
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ public Object evaluate(String pptrFunction, List<Object> args) throws JsonProces
public ElementHandle waitForSelector(String selector, WaitForSelectorOptions options) {
QuerySelector querySelector = QueryHandlerUtil.getQueryHandlerAndSelector(selector);
options.setPolling(querySelector.getPolling());
return querySelector.getQueryHandler().waitFor(this, selector, options);
return querySelector.getQueryHandler().waitFor(this, querySelector.getUpdatedSelector(), options);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public class CdpElementHandle extends ElementHandle {

private static final Set<String> NON_ELEMENT_NODE_ROLES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList("StaticText", "InlineTextBox")));
private Integer backendNodeId;

CdpElementHandle(IsolatedWorld world, RemoteObject remoteObject) {
super(new CdpJSHandle(world, remoteObject));
}
Expand Down Expand Up @@ -143,7 +144,7 @@ public void autofill(AutofillData data) {

@Override
public int backendNodeId() {
if(Objects.nonNull(this.backendNodeId)){
if (Objects.nonNull(this.backendNodeId)) {
return this.backendNodeId;
}
try {
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/com/ruiyun/jvppeteer/cdp/core/CdpJSHandle.java
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
package com.ruiyun.jvppeteer.cdp.core;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.ruiyun.jvppeteer.api.core.CDPSession;
import com.ruiyun.jvppeteer.api.core.ElementHandle;
import com.ruiyun.jvppeteer.api.core.JSHandle;
import com.ruiyun.jvppeteer.api.core.Realm;
import com.ruiyun.jvppeteer.cdp.entities.RemoteObject;
import com.ruiyun.jvppeteer.common.Constant;
import com.ruiyun.jvppeteer.exception.EvaluateException;
import com.ruiyun.jvppeteer.exception.JvppeteerException;
import com.ruiyun.jvppeteer.util.Helper;
import com.ruiyun.jvppeteer.util.StringUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;

/**
* 表示对 JavaScript 对象的引用。可以使用 Page.evaluateHandle() 创建实例。
Expand All @@ -36,6 +41,21 @@ public boolean disposed() {
return disposed;
}

@Override
public Map<String, JSHandle> getProperties() throws JsonProcessingException {
JsonNode response = this.client().send("Runtime.getProperties", Constant.OBJECTMAPPER.createObjectNode().put("objectId", this.remoteObject.getObjectId()).put("ownProperties", true));
Map<String, JSHandle> result = new LinkedHashMap<>();
Iterator<JsonNode> iterator = response.get(Constant.RESULT).iterator();
while (iterator.hasNext()) {
JsonNode property = iterator.next();
if (!property.get("enumerable").asBoolean() || !property.hasNonNull("value")) {
continue;
}
result.put(property.get("name").asText(), this.world.createJSHandle(Constant.OBJECTMAPPER.treeToValue(property.get("value"), RemoteObject.class)));
}
return result;
}

@Override
public Realm realm() {
return this.world;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ private JsonNode convertArgument(ExecutionContext context, Object arg) {
objectNode.put("unserializableValue", NaN);
return objectNode;
}
CdpJSHandle objectHandle = arg instanceof CdpJSHandle ? (CdpJSHandle) arg : null;
JSHandle objectHandle = arg instanceof JSHandle ? (JSHandle) arg : null;
if (objectHandle != null) {
if (objectHandle.realm() != context.world()) {
throw new JvppeteerException("JSHandles can be evaluated only in the context they were created!");
Expand Down
20 changes: 11 additions & 9 deletions src/main/java/com/ruiyun/jvppeteer/cdp/entities/Binding.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
package com.ruiyun.jvppeteer.cdp.entities;

import com.ruiyun.jvppeteer.api.core.JSHandle;
import com.ruiyun.jvppeteer.common.BindingFunction;
import com.ruiyun.jvppeteer.cdp.core.CdpJSHandle;
import com.ruiyun.jvppeteer.cdp.core.ExecutionContext;
import com.ruiyun.jvppeteer.common.BindingFunction;
import com.ruiyun.jvppeteer.exception.EvaluateException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -43,16 +44,16 @@ public void run(ExecutionContext context, int id, List<Object> args, boolean isT
" }", params);
try {
Map<String, JSHandle> properties = handles.getProperties();
AtomicInteger count = new AtomicInteger();
properties.forEach((key, handle) -> {
if (args.contains(key)) {
if (handle.remoteObject().getSubtype().equals("node")) {
args.set(Integer.parseInt(key), handle);
if (count.get() <= args.size()) {
if (Objects.equals(handle.remoteObject().getSubtype(), "node")) {
args.set(count.get(), handle);
} else {
handle.dispose();
}
} else {
handle.dispose();
}
count.getAndIncrement();
});
} finally {
if (handles != null) {
Expand All @@ -72,11 +73,12 @@ public void run(ExecutionContext context, int id, List<Object> args, boolean isT
" }", params);

for (Object arg : args) {
if (arg instanceof CdpJSHandle) {
((CdpJSHandle) arg).dispose();
if (arg instanceof JSHandle) {
((JSHandle) arg).dispose();
}
}
} catch (Exception e) {
LOGGER.error("jvppeteer error: ", e);
if (e instanceof EvaluateException) {
try {
List<Object> params = new ArrayList<>();
Expand Down

0 comments on commit 4ace9f0

Please sign in to comment.