I have an application developed by using Vaadin Framework, Now i need to click on the rectangular polygon which is on the Canvas.following is the html code
here i am providing the Html code
<canvas width="1920" height="524" class="ol-unselectable" style="width: 100%; height: 100%;"></canvas>
and i tried by using Actions which makes the mouse move over the Polygon and click .
int x = (int) 5638326.333511386;
int y = (int) 2580101.9711508946;
driver.get("http://localhost:8080/internship");
WebElement ele = driver.findElement(By.xpath("//canvas[#class='ol-unselectable']"));
// driver.findElement(By.tagName("canvas"));
//driver.findElemet(By.className("ol-unselectable"));
try {
Actions builder = new Actions(driver);
builder.moveToElement(ele, x, y);
builder.clickAndHold();
builder.release();
builder.perform();
} catch (Exception e) {
// do nothing
}
i am getting the foloowing error
org.openqa.selenium.NoSuchElementException: Unable to locate element:
//canvas[#class='ol-unselectable'].
can anyone suggest some samples how to find polygon on canvas with co-ordinates and make click on it.
Usually, the canvas element is embedded in an iframe.
So, first, you have to find the iframe element and then find the canvas inside the iframe. For instance:
WebDriver driver = new FirefoxDriver(firefoxOptions);
try {
driver.get("https://www.w3schools.com/html/tryit.asp?filename=tryhtml5_canvas_empty");
WebElement iframe = driver.findElement(By.name("iframeResult"));
driver.switchTo().frame(iframe);
WebElement canvas = driver.findElement(By.id("myCanvas"));
System.out.println(canvas.getText());
} finally {
driver.quit();
}
I think this code might help you.
EDIT:
After chatting with #RamanaMuttana and his changes on the posted question, I could better understand his need.
We realized that just using the By.tagName selector was enough to find the canvas element as in the code bellow:
driver.findElements(By.tagName("canvas")).get(0);
Related
I'm trying to take screenshots of a video using Selenium and Java. It works fine when I use headed Chrome, but when I want to make it headless a "java.awt.image.RasterFormatException: (y + height) is outside of Raster" is thrown in
getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
Here's the code:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
// Get URL and find image
driver.get("https://www.webcamtaxi.com/en/netherlands/north-holland/purmerend-traffic-cam.html");
Thread.sleep(5000);
while (true) {
WebElement ele = driver.findElement(By.id("insideCam"));
// Get entire page screenshot
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = ele.getLocation();
// Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
}
Does someone know how to fix this?
I have created 'takescreenshot' reusable method to capture screenshot and have been calling wherever I need it.
however I am facing one strange issue here. Every time this function is called the captured image size keeps increasing
e.g 252K -> 278K -> 310K -> 400K ...
These captured images I am using in ExtentReport. Also apart from selenium session image, I do see a black background image being captured not sure where it is coming from.
method code is as below:
public static void takescreenshot(ExtentTest Test,String Status){
Date d=new Date();
String CurrentTimeStamp=d.toString().replace(":", "_").replace(" ", "_");
File scrFile =((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File(CurrentTimeStamp+".png"));
if(Status.equals("pass")){
Test.log(LogStatus.PASS, "snapshot below:-"+CurrentTimeStamp+".png"));
}else if(Status.equals("fail")){
Test.log(LogStatus.FAIL, "snapshot below:-"+CurrentTimeStamp+".png"));
}
}
if I hardcode some existing image in extentreport code then everything works fine.
Has anyone come across this issue ever.
I had written a code to capture screenshot of elements which works fine. May be this will help you. I don't know what Extendreport is, so can't help you there.
public static void takeElementScreenshot(WebDriver driver, WebElement element){
try{
// Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = ImageIO.read(screenshot);
// Get the location of element on the page
Point point = element.getLocation();
// Get width and height of the element
int eleWidth = element.getSize().getWidth();
int eleHeight = element.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
eleWidth, eleHeight);
ImageIO.write(eleScreenshot, "png", screenshot);
// Copy the element screenshot to disk
File screenshotLocation = new File("D:\\Screenshot.png");
FileUtils.copyFile(screenshot, screenshotLocation);
}
catch(Exception e){
}
}
NOTE: I am aware this question has been asked before a few times however I am having a problem that others seem to not be having.
Even though I appear to be getting the point of the element along with its width and height correctly, the final crop is incorrect. It is like the screenshot I am taking has different dimensions to the webpage. I am using the Chrome Driver.
This is my code for attempting to get a screenshot of the google logo image:
WebDriver driver = new ChromeDriver();
driver.get("http://www.google.com");
WebElement ele = driver.findElement(By.id("hplogo"));
//Get entire page screenshot
File screenshot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
}
//Get the location of element on the page
Point point = ele.getLocation();
//Get width and height of the element
int eleWidth = ele.getSize().getWidth();
int eleHeight = ele.getSize().getHeight();
//Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth,
eleHeight);
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e) {
}
//Copy the element screenshot to disk
File screenshotLocation = new File("/Users/M/Desktop/stuff/logo.png");
try{
FileUtils.copyFile(screenshot, screenshotLocation);
}catch(IOException e){
}
Here is my final logo image:
Here is what it should be getting (just as a png not as gif):
Does anyone have any idea what might be going on?
Check if you have custom scaling on your operating system. I had the same problem using 125% scaling on windows.
Refer to the below screenshot for more details.
Go to your system settings > Search for the display > change the value of "Make Everything Bigger" dropdown to 100%. Then run the code again this will work fine for you.
I am trying to write selenium tests for a website using java. However, I have come across a problem when testing file uploading.
When I click the file upload button, it automatically opens the windows file upload. I have code working to put the file path ("D:\\test.txt") into selection. From researching this subject I understand there is no way for selenium webdriver to handle this. So my question is this: what is a way I can simply close the upload window in an automated way? Indeed the sendKeys working with selecting the txt file but the window upload still not closing.
Thanks in advance
ProductActionCode :
public static void AutoInsert_Execute(WebDriver driver) throws Exception {
ConfirmationPlaceBet_Page.btn_ChanShiOdd(driver).click();
ConfirmationPlaceBet_Page.btn_UploadOddTxt(driver).click();
ConfirmationPlaceBet_Page.btn_DocumentToBeUpload(driver).click();
ConfirmationPlaceBet_Page.pick_DocumentToBeUpload(driver).sendKeys("D:\\test.txt");
ConfirmationPlaceBet_Page.btn_ProceedUploadAuto(driver).click();
ConfirmationPlaceBet_Page.btn_ConfirmedUploadAuto(driver).click();
for (int i = 0; i < 3; i++) {
ConfirmationPlaceBet_Page.btn_AddDoubleBet(driver).click();
}
ConfirmationPlaceBet_Page.btn_ConfirmNumberToBet(driver).click();
for (int k = 0; k < 49; k++) {
ConfirmationPlaceBet_Page.btn_IncreaseBet(driver).click();
}
ConfirmationPlaceBet_Page.btn_ProceedBet(driver).click();
ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();
}
ConfirmationPlaceBetCode :
public static WebElement pick_DocumentToBeUpload(WebDriver driver) throws Exception{
try{
driver.manage().timeouts().implicitlyWait(200, TimeUnit.SECONDS);
element = driver.findElement(By.name("file"));
Thread.sleep(500);
//Log.info("Pick Lottery1 ");
}catch (Exception e){
Log.error("Button is not found on the Confirmation Page");
throw(e);
}
return element;
}
HTML CODE :
<div id="filePicker" class="webuploader-container"><div class="webuploader-pick">选择文件</div><div id="rt_rt_1a24olu914nt122e1qls1c5l1b2qm" style="position: absolute; top: 0px; left: 0px; width: 86px; height: 30px; overflow: hidden; bottom: auto; right: auto;"><input type="file" name="file" class="webuploader-element-invisible" multiple="multiple" accept="text/*"><label style="opacity: 0; width: 100%; height: 100%; display: block; cursor: pointer; background: rgb(255, 255, 255);"></label></div></div>
You need to use sendkeys for same.
I assuming that you have a browse button and a button as upload
driver.findElement(By.xpath("YOUR XPATH")).sendKeys("Absolute path of file");
Feel free to change locator of an element in the above code
sendkeys will set the path and file name in the HTML for the respective upload field
Now click on upload button.
ConfirmationPlaceBet_Page.btn_ConfirmBet(driver).click();
Note:- put a wait between sendkeys and click on upload button. It helps many times
For more info refer below link:-
http://seleniumeasy.com/selenium-tutorials/uploading-file-with-selenium-webdriver
Hope it will help you :)
You can use these lines of code to close the upload window after sending the path of file with pressing ESCAPE Button
import java.awt.Robot;
import java.awt.event.KeyEvent;
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
For Example look at below code:
public static void main(String[] args) throws InterruptedException, AWTException {
System.setProperty("webdriver.gecko.driver", "D:\\geckodriver-v0.19.1-win64\\geckodriver.exe");
WebDriver driver = new FirefoxDriver();
driver.get("https://online2pdf.com/reduce-pdf-file-size");
WebElement element =driver.findElement(By.xpath("//*[contains(text(),'Select files')]"));
Actions ac = new Actions(driver);
ac.moveToElement(element).click().build().perform();
driver.switchTo().activeElement().sendKeys("C:\\Users\\eclipse-workspace\\Selenium\\abc.pdf");
Thread.sleep(300);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_ESCAPE);
robot.keyRelease(KeyEvent.VK_ESCAPE);
}
Hi all IM having troubles. My test click an hyperlynk and a new window is displayed. the problem is that when I implement windows handlers selenium only identify one window instead of 2 window and when I try to find the element of the window, well I cant cause for selenium the window never exist :(
driver.switchTo().frame(CQLo.getMframe());
WebElement Rad_CIT = (new WebDriverWait(driver,10)).until(ExpectedConditions.elementToBeClickable(CQLo.getRadCIT()));
Rad_CIT.click();
WebElement Text_CIT = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(CQLo.getTextCIT()));
Text_CIT.clear();
Text_CIT.sendKeys(citbox);
setLog("CITBox: " +citbox);
//WebElement Link_WFM =
Actions act = new Actions(driver);
WebElement onElement = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(CQLo.getLink_WFM_Admin()));
act.contextClick(onElement).perform();
act.sendKeys("o").perform();
Set <String> wind_ows = driver.getWindowHandles();
for(String sw : wind_ows)
{ System.out.println(""+sw.toString());}
setLog("Open log on " +citbox);
Thread.sleep(2000);
WebElement Text_UserN = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(Elog.getUserName()));
Text_UserN.sendKeys(MSR_name);
WebElement Text_UserP = (new WebDriverWait(driver, 10)).until(ExpectedConditions.presenceOfElementLocated(Elog.getUserPassword()));
Text_UserP.sendKeys(MSR_pass);
WebElement B_logon = (new WebDriverWait(driver, 10)).until(ExpectedConditions.elementToBeClickable(Elog.getB_logon()));
B_logon.click();
after printing the SET with the windows I only receive one window
Wait some time before you get window handles after performing click on element. I think by the time you read driver window handles, new window might not be opened.