-3

I had used JavaScript-executer & was able to find the ImageElement, But further when I try to extract the URL(for downloading the image.svg ), I am getting an error "NoSuchElementException ", below is my Code:

((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))
J Fabian Meier
  • 33,516
  • 10
  • 64
  • 142
samrac
  • 1
  • 2

1 Answers1

0

Problem Explanation :

The first line of your code is

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

which basically means is you are trying to open https://www.lambdatest.com/selenium-automation/ in a new tab. Selenium does not know until you tell this to Selenium that you have to switch to different tab now for further execution.

So, your line of code should be something like this

((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));

Update 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);
cruisepandey
  • 28,520
  • 6
  • 20
  • 38
  • if that is the case , then How does the last line work ? – samrac Sep 07 '21 at 06:07
  • is that open a new window ? or do you have `driver.get()` not before this ? why would one use js to open a new windows. also mention which last line you are referring to ? – cruisepandey Sep 07 '21 at 06:08
  • This is one of the Segment of complete Code. I already have one tab open for another page previously(using driver.get() ). Here, i have opened new tab for this link & trying to download the image. – samrac Sep 07 '21 at 06:12
  • Did you try to switch like I have suggested ? if not, do that first and share the entire error stack trace if there's any – cruisepandey Sep 07 '21 at 06:13
  • Also how did you conclude that this line is working ? is it this line `js.executeScript("window.scrollTo(0, document.body.scrollHeight)"); ` ? – cruisepandey Sep 07 '21 at 06:29
  • I can see , the Webpage is Scrolled to the End – samrac Sep 07 '21 at 08:30
  • ideally it should not, cause you are opening a new tab and not switching to it. so it should not. – cruisepandey Sep 07 '21 at 08:31
  • Here the Entire Code, Please have a look ; https://github.com/sam-rac/Lambdatest_Test_1/blob/master/src/test/java/Page/LoginPage.java – samrac Sep 07 '21 at 08:48
  • @samrac : See the updated 1 code above, it works for me – cruisepandey Sep 07 '21 at 09:46