Skip to content

Commit

Permalink
Fix bug: when cannot find elements, switch back to the original handl…
Browse files Browse the repository at this point in the history
…e instead of one of the first two handles
  • Loading branch information
naibo committed Jan 6, 2025
1 parent 9efd3b6 commit 2cf3379
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
9 changes: 8 additions & 1 deletion ElectronJS/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ async function findElementAcrossAllWindows(
let handles = await driver.getAllWindowHandles();
// console.log("handles", handles);
let content_handle = current_handle;
let old_handle = current_handle;
let id = -1;
try {
id = msg.message.id;
Expand Down Expand Up @@ -310,7 +311,7 @@ async function findElementAcrossAllWindows(
if (h != null && handles.includes(h)) {
await driver.switchTo().window(h);
current_handle = h;
console.log("switch to handle: ", h);
console.log("Switch to handle: ", h);
}
element = await findElement(driver, By.xpath, xpath, iframe);
break;
Expand All @@ -327,6 +328,12 @@ async function findElementAcrossAllWindows(
}
}
if (element == null && notifyBrowser) {
// 如果找不到元素,切换回原来的窗口
if (old_handle != null && handles.includes(old_handle)) {
await driver.switchTo().window(old_handle);
current_handle = old_handle;
console.log("Switch to handle: ", old_handle);
}
notify_browser(
"无法找到元素,请检查XPath是否正确:" + xpath,
"Cannot find the element, please check if the XPath is correct: " + xpath,
Expand Down
1 change: 1 addition & 0 deletions ElectronJS/tasks/326.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"id":326,"name":"动态调试测试","url":"https://www.baidu.com","links":"https://www.baidu.com","create_time":"2025-01-06 13:08:59","update_time":"2025-01-06 13:10:30","version":"0.6.3","saveThreshold":10,"quitWaitTime":60,"environment":0,"maximizeWindow":0,"maxViewLength":15,"recordLog":1,"outputFormat":"csv","saveName":"current_time","dataWriteMode":1,"inputExcel":"","startFromExit":0,"pauseKey":"p","containJudge":false,"browser":"chrome","removeDuplicate":0,"desc":"https://www.baidu.com","inputParameters":[{"id":0,"name":"urlList_0","nodeId":1,"nodeName":"打开网页","value":"https://www.baidu.com","desc":"要采集的网址列表,多行以\\n分开","type":"text","exampleValue":"https://www.baidu.com"}],"outputParameters":[{"id":0,"name":"参数2_文本","desc":"","type":"text","recordASField":1,"exampleValue":"联播+|从“常”“长”二字读懂反腐倡廉"}],"graph":[{"index":0,"id":0,"parentId":0,"type":-1,"option":0,"title":"root","sequence":[1,2,4,5],"parameters":{"history":1,"tabIndex":0,"useLoop":false,"xpath":"","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0},"isInLoop":false},{"id":1,"index":1,"parentId":0,"type":0,"option":1,"title":"打开网页","sequence":[],"isInLoop":false,"position":0,"parameters":{"useLoop":false,"xpath":"","wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"url":"https://www.baidu.com","links":"https://www.baidu.com","maxWaitTime":10,"scrollType":0,"scrollCount":1,"scrollWaitTime":1,"cookies":""}},{"id":2,"index":2,"parentId":0,"type":0,"option":2,"title":"点击从“常”“...","sequence":[],"isInLoop":false,"position":1,"parameters":{"history":4,"tabIndex":-1,"useLoop":false,"xpath":"//*[@id=\"hotsearch-content-wrapper\"]/li[1]/a[1]","iframe":false,"wait":2,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"scrollType":0,"scrollCount":1,"scrollWaitTime":1,"clickWay":0,"newTab":0,"maxWaitTime":10,"params":[],"alertHandleType":0,"downloadWaitTime":3600,"allXPaths":["/html/body/div[1]/div[1]/div[5]/div[1]/div[1]/div[3]/ul[1]/li[1]/a[1]","//a[contains(., '0从“常”“长”二')]","//a[@class='title-content c-link c-font-medium c-line-clamp1']","/html/body/div[last()-4]/div[last()-3]/div[last()-3]/div/div/div/ul/li[last()-9]/a"]}},{"id":-1,"index":3,"parentId":0,"type":0,"option":3,"title":"提取数据","sequence":[],"isInLoop":false,"position":2,"parameters":{"history":1,"tabIndex":-1,"useLoop":false,"xpath":"","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"clear":0,"newLine":1,"params":[{"nodeType":0,"contentType":0,"relative":false,"name":"参数1_文本","desc":"","extractType":0,"relativeXPath":"/html/body/div[3]/div[4]/div[1]/div[3]/div[2]/div[1]123","allXPaths":["/html/body/div[3]/div[4]/div[1]/div[3]/div[2]/div[1]","//div[contains(., '联播+|从“常”“长')]","/html/body/div[last()-6]/div/div[last()-5]/div[last()-2]/div[last()-9]/div"],"exampleValues":[{"num":0,"value":"联播+|从“常”“长”二字读懂反腐倡廉昨天17:16 - 恒者行远,思者常新。2024年终岁末,中共中央政治局召开民主生活会,习近平总书记主持会议并发表重要讲话。他以高度的历史清醒强调,只要滋生腐败的土壤和条件仍然存在,反腐...央视网\n \n \n 播报\n \n \n \n 暂停\n \n"}],"unique_index":"5lnmjsgh2ccm5kl0qmt","iframe":false,"default":"","paraType":"text","recordASField":1,"beforeJS":"","beforeJSWaitTime":0,"JS":"","JSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"downloadPic":0,"splitLine":0}]}},{"id":3,"index":4,"parentId":0,"type":0,"option":2,"title":"点击从“常”“长...","sequence":[],"isInLoop":false,"position":2,"parameters":{"history":1,"tabIndex":-1,"useLoop":false,"xpath":"//*[contains(@class, \"c-gap-bottom-small\")]/a[1]/em[1]","iframe":false,"wait":2,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"scrollType":0,"scrollCount":1,"scrollWaitTime":1,"clickWay":0,"newTab":0,"maxWaitTime":10,"params":[],"alertHandleType":0,"downloadWaitTime":3600,"allXPaths":["/html/body/div[3]/div[4]/div[1]/div[3]/div[2]/div[1]/h3[1]/a[1]/em[1]","//em[contains(., '从“常”“长”二字读')]","/html/body/div[last()-6]/div/div[last()-5]/div[last()-2]/div[last()-9]/div/h3/a/em"]}},{"id":4,"index":5,"parentId":0,"type":0,"option":3,"title":"提取数据","sequence":[],"isInLoop":false,"position":3,"parameters":{"history":1,"tabIndex":-1,"useLoop":false,"xpath":"","iframe":false,"wait":0,"waitType":0,"beforeJS":"","beforeJSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"waitElement":"","waitElementTime":10,"waitElementIframeIndex":0,"clear":0,"newLine":1,"params":[{"nodeType":0,"contentType":0,"relative":false,"name":"参数2_文本","desc":"","extractType":0,"relativeXPath":"/html/body/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]","allXPaths":["/html/body/div[1]/div[1]/div[1]/div[2]/div[1]/div[1]/div[1]/div[1]/div[1]","//div[contains(., '联播+|从“常”“长')]","//div[@class='sKHSJ']","/html/body/div[last()-4]/div/div/div[last()-2]/div[last()-1]/div[last()-1]/div[last()-2]/div/div[last()-1]"],"exampleValues":[{"num":0,"value":"联播+|从“常”“长”二字读懂反腐倡廉"}],"unique_index":"kwcdnkske6m5kl2c72","iframe":false,"default":"","paraType":"text","recordASField":1,"beforeJS":"","beforeJSWaitTime":0,"JS":"","JSWaitTime":0,"afterJS":"","afterJSWaitTime":0,"downloadPic":0,"splitLine":0}]}}]}

0 comments on commit 2cf3379

Please sign in to comment.