-3

我使用了 JavaScript 执行器并能够找到ImageElement,但是当我尝试提取 URL(用于下载 image.svg)时,我收到错误“NoSuchElementException”,下面是我的代码:

((JavascriptExecutor)driver).executeScript("window.open('https://www.lambdatest.com/selenium-automation/','_blank');");                     
wait.until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));         
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));  
JavascriptExecutor js = (JavascriptExecutor) driver;   

By Image=By.xpath("//img[@title='Jenkins']");            
js.executeScript("window.scrollTo(0, document.body.scrollHeight)");           
      //  Below commented snippet is giving Error 
/*WebElement ImageElemnt =driver.findElement(Image); 
   String src = ImageElemnt.getAttribute("src");    
    System.out.println(src);*/



wait.ignoring(NoSuchElementException.class).until(ExpectedConditions.invisibilityOfElementLocated(Image))

在正式修补之前,这似乎是 Capacitor 2 的可行解决方案:

更新的答案

截至 2021 年 9 月 10 日,Capacitor v2.5.0 包含下面概述的补丁,但建议尽可能升级到 Capacitor v3,它也包含一个补丁,并且将收到与 Capacitor v2.x 不同的错误补丁。

原始答案

现在我已经手动对#4992进行 了更改node_modules/@capacitor/android/capacitor/src/main/java/com/getcapacitor/plugin/Geolocation.java 并使用了补丁包,以便我们可以修复我们的应用程序。如果修复在 Capacitor v2 中或当我们升级到 v3 的那一天到来时,我们可以删除补丁。

我们刚刚使用patch-package应用的步骤:

  1. 使用此 PR 更改访问@capacitor/android和更新( https://github.com/ionic-team/capacitor/pull/4992/files )Geolocation.java
  2. npx patch-package @capacitor/android
  3. 更新package.json_"postinstall": "patch-package"
  4. npm install patch-package因此npm install在 Ionic AppFlow 期间运行时存在依赖关系
  5. 提交补丁文件和更改package.json
  6. (可选)如果您收到“无法在 wd [...] 中运行”之类的错误,请在无法使用非 root 用户时添加.npmrcwith ,例如在使用 Ionic AppFlow 构建和部署时unsafe-perm = true
4

1 回答 1

0

问题说明:

您的代码的第一行是

((JavascriptExecutor)driver).executeScript("window.open('https://www.lambdatest.com/selenium-automation/','_blank');");  

这基本上意味着您正在尝试https://www.lambdatest.com/selenium-automation/在新选项卡中打开。Selenium 不知道,直到您告诉 Selenium 您现在必须切换到不同的选项卡才能进一步执行。

所以,你的代码行应该是这样的

((JavascriptExecutor)driver).executeScript("window.open('https://www.lambdatest.com/selenium-automation/','_blank');");  
ArrayList<String> tabs2 = new ArrayList<String> (driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));

更新 1:

((JavascriptExecutor) driver).executeScript("window.open('https://www.lambdatest.com/selenium-automation/','_blank');");
ArrayList<String> tabs2 = new ArrayList<String>(driver.getWindowHandles());
driver.switchTo().window(tabs2.get(1));
Thread.sleep(5000);
WebElement ele = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//img[@title='Jenkins']")))
js.executeScript("arguments[0].scrollIntoView(true);", ele);
String src = ele.getAttribute('src')
System.out.println(src);
于 2021-09-07T05:57:18.393 回答