Selniuem webdriver java(Drag and Drop issue) - java

I have added robot class to drag from left to right in the frame but it is not working I also use action builder drag and drop it is not working somehow I use robot class so it clicks on the header but does not do drag and drop
Here is my code :
WebDriverWait waitDrag = new WebDriverWait(driver, Duration.ofSeconds(30));
waitDrag.until(ExpectedConditions.visibilityOfAllElementsLocatedBy(By.xpath("//*[#id=\"root\"]/div/div/div[2]/div[2]/div/div[1]/div[2]/div/div[1]/div/div/div/div[1]/div")));
WebElement fromWebElement =driver.findElement(By.xpath("//*[#id=\"root\"]/div/div/div[2]/div[2]/div/div[1]/div[2]/div/div[1]/div/div/div/div[1]/div"));
WebElement toWebElement = driver.findElement(By.xpath("//div[#class=\"react-flow__pane\"]"));
Point coordinates1 = fromWebElement.getLocation();
Point coordinates2 = toWebElement.getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates1.getX(), coordinates1.getY());
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseMove(coordinates2.getX(), coordinates2.getY());
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Thread.sleep(2000);

Related

Java Selenium "Element Not Interactable Exception" when using sendKeys to open multiple tabs

I am trying to web scrape a Quebec government website for law names and their associated PDFs but when I try to open the tabs of all the different laws to get their PDF links, I get an ElementNotInteractable Exception when it attempts to open the 9th link. I tried opening the link by itself and it opens fine but when it is going through all the laws, it stops there and gives me that exception. Here is my code snippet:
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
Thread.sleep(5000);
List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.xpath("//body/div/div/div[2]/div/div[2]/table/tbody/tr[contains(#class, 'clickable')]/td/a"));
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedStatutes.size(); i++){
String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
}
There are several issues here:
The main problem is that you have to scroll the element you want to click on into the view. Your default initial screen height presents 8 rows while to click on 9-th row and more you have to scroll that element first into the view.
You could set driver window to better dimensions, this will show you more screen, however you will still have to scroll, but after 15 elements.
You should improve your locators.
You should not mix up WebDriverWait and implicitlyWait.
This should work better:
static SortedMap<String,String> QuebecConsolidatedStatutesAndPDFs = new TreeMap<String,String>();
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\WorkSpace\\Driver\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(5000));
driver.manage().window().maximize();
driver.get("http://www.legisquebec.gouv.qc.ca/en/chapters?corpus=statutes&selection=all");
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("tr.clickable a"), 100));
Thread.sleep(300);
List<WebElement> QuebecConsolidatedStatutes = driver.findElements(By.cssSelector("tr.clickable a"));
String parent = driver.getWindowHandle();
for (int i=0; i<QuebecConsolidatedStatutes.size(); i++){
String opentabs = Keys.chord(Keys.CONTROL, Keys.ENTER);
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", QuebecConsolidatedStatutes.get(i));
Thread.sleep(300);
wait.until(ExpectedConditions.visibilityOf(QuebecConsolidatedStatutes.get(i)));
QuebecConsolidatedStatutes.get(i).sendKeys(opentabs);
}
}

How to switch to a new window and print the title in selenium webdriver (without windowhandles)

Iam new to selenium and trying to switch to new amazon tab which I opened and print the title
But when Sending control and Tab nothing happens how to achieve it can anyone help here :)
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.get("https://www.amazon.com");
//Thread.sleep(6000);
Actions a = new Actions(driver);
List<WebElement> el= driver.findElements(By.xpath("(//div[#class='navFooterColHead'])[1]/../ul/li/a"));
for(int i=0;i<el.size();++i)
{
el.get(i).sendKeys(Keys.Control,Keys.Enter);
}
driver.findElement(By.cssSelector("body")).sendKeys(Keys.CONTROL +"\t");

Unable to locate the Element on Canvas using Selenium WebDriver

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

Switch control to new window when new window is opening only after closing the existing window

How to switch control to new window if new window is opening only after closing the existing window?
I am using Selenium and Internet Explorer.
for (String h : webDriver.getWindowHandles()) {
webDriver.switchTo().window(h);}
WebElement element= webDriver.findElement(By.xpath("//*[#id='Standard']/p[1]"));
Actions a = new Actions(webDriver);
a.moveToElement(element).build().perform();

Selenium does not recognize a new opened window

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.

Categories

Resources