Drag an element over another in selenium - selenium

I am trying to drag and drop an element over another but I am not able to do so. I am trying to perform the above operation on http://the-internet.herokuapp.com/drag_and_drop
This is my code
we1=driver.findElement(By.xpath("//*[#id='column-a']"));
we2=driver.findElement(By.xpath("//*[#id='column-b']"));
action.clickAndHold(we1);
action.moveToElement(we2, 450, 250);
Thread.sleep(3000);
action.release().build().perform();
Instead of action.moveToElement(we2,450,250) I have even tried using action.moveToElement(we2) and action.moveByOffset(450,250).
I even tried to perform this operation using Robot class.
robot.mouseMove(230, 170);
Thread.sleep(3000);
robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
robot.mouseMove(450, 200);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
Nothing happens in any of the above code. No error or exception is thrown and Code execution gets completed successfully.

After a lot of effort (hit & try :) ) I was able to find a solution for my problem. It is working fine for the site I mentioned as well as for other sites also where drag and drop functionality is present. Here is the solution:
we1CenterX=we1Points.x+we1Dimensions.width/2;
we1CenterY=we1Points.y+we1Dimensions.height/2;
we2CenterX=we2Points.x+we2Dimensions.width/2;
we2CenterY=we2Points.y+we2Dimensions.height/2;
robot.mouseMove(we1CenterX, we1CenterY);
waitForMe(1); //custom wait
robot.mousePress(InputEvent.BUTTON1_MASK);
waitForMe(1);
robot.mouseMove(we2CenterX, we2CenterY);
waitForMe(1);
robot.mouseMove(we2CenterX+100, we2CenterY-70);
/*Moving the cursor over the element before releasing
the mouse button solved my problem*/
waitForMe(1);
robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
waitForMe(1);

Related

Unable to switch to frame in loop in selenium

`I am trying to switch to particular frame using selenium python in loop. It gets executed the first time but after that gives timeout exception error. Here is the code:
Navigate.py:
def navigate_to_frame():
driver.switch_to.default_content()
//Navigation to the frame using execute script
action.py
def perform():
driver.switch_to.default_content()
driver.switch_to.frame(frame)
//Perform required actions
Start.py
for i in range(0,5):
navigate_to_frame()
perform()
The above code works for first time but in the second iteration it throws an exception
I found the solution somewhere else. Came back to answer it.
You are still in the first frame from your first iteration when you try to select from the second iteration.
You need to leave the frame it at the end of the first loop to reset where you are looking at. For example with. a "select frame relative=top".

Selenium code freezes after opening second window

Following code hangs for about 12 minutes after clicking on "Print Change"
button and then throws error that element not visible at line:
driver.findElement(By.xpath("//button[contains(text(),'Print
Change')]")).click();
System.setProperty("webdriver.chrome.driver",
System.getProperty("user.dir")+ "\\exe\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
driver.manage().timeouts().implicitlyWait(120, TimeUnit.SECONDS);
driver.get(prop.getProperty("https://genpact-qa-smartit.onbmc.com"));
driver.findElement(By.xpath("//*[#id='okta-signin-
username']")).sendKeys(userid);
driver.findElement(By.xpath("//*[#id='okta-signin-
password']")).sendKeys(pwd);
driver.findElement(By.xpath("//*[#id='okta-signin-submit']")).click();
driver.findElement(By.xpath("//a[#id='header-search_button']")).click();
driver.findElement(By.xpath("//input[#id='globalSearchBox']")).
sendKeys("CRQ000000029504");
Robot robot=new Robot();
robot.keyPress(KeyEvent.VK_ENTER);
robot.keyRelease(KeyEvent.VK_ENTER);
driver.findElement(By.xpath("//a[text()='View Full Change']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//div[#title='Print']")).click();
Thread.sleep(3000);
driver.findElement(By.xpath("//button[contains(text(),'Print
Change')]")).click();
System.out.println("Clicked on Print Change button");
I am using OS: Windows 10; Browser: Chrome 77.0.3865.90; Selenium: 3.141.59
Any help would be highly appreciated.
Regards,
Surender
First of all for your safety please edit your post removing your username and password that are in your code (and afterwards change them).
As far as your problem is concerned there is a high possibility that:
driver.findElement(By.xpath("//button[contains(text(),'Print
Change')]")).click();
there are more than one elements with the given xpath so the one you want might not be visible.
Therefore you should either be more specific with the:
[contains(text(),' ... ']
or try using the css selectors.
Anyway look up the docs for locating elements and if you still don't find a solution I will be happy to help you again.
Edit: In order to help you I logged in and your solution probably is to replace
driver.findElement(By.xpath("//button[contains(text(),'Print
Change')]")).click();
with
driver.findElement(By.xpath("/html/body/div[4]/div/div/div/div[3]/div/button[1])]")).click();
You specified an implicit wait of 120 seconds , so the code will wait for 120 seconds before throwing in to an exception.
in this case i think the xpath is wrong that's why its waiting for long time (may be there is some extra space in between the text print change). Use any xpath identification tool to check whether the xpath is correct or not
i hope the following code may work
driver.findElement(By.xpath("//button[contains(text(),'Print')]")).click();

Robot Framework: wait until element disappears

I run a test where I have to dismiss a warning that is a DIV that overlays the whole page. First I send a click to a button that adds something, then a warning is displayed. Finally I enter a string in a field
The code looks something like this
SeleniumLibrary.Click element add_button
Wait Until Element is visible warning-overlay-div
SeleniumLibrary.Click element dismiss-warning
SeleniumLibrary.Click element something-else
Running this code results in an error message:
WebDriverException: Message: unknown error: Element <input
type="text" class="upperCaseClass modified" id="something-else"
maxlength="15" style="width: 143px; text-transform: uppercase;"> is
not clickable at point (230, 679). Other element would receive the
click: <div class="warning-overlay-div" style="width: 100%; height:
853px; z-index: 2003;"></div>
That is, I can't click in something-else because warning-overlay-div is on top of it.
Ok, so I added a check to make sure the warning-overlay-div is gone between the the click dismiss-warning and the click on something-else. I have tried multiple variants but these three all give the same result
(from the log)
00:00:15.050KEYWORD SeleniumLibrary . Wait Until Element Is Not Visible ${warning-overlay-div}
00:00:15.003KEYWORD SeleniumLibrary . Wait Until Page Does Not Contain ${warning-overlay-div}
00:00:15.039KEYWORD SeleniumLibrary . Wait Until Page Does Not Contain Element ${warning-overlay-div}
It might take half a second or so for the overlay to disappear but as you can see, all these wait until they timeout after 15 seconds before they return success. I want to continue as soon as the warning-overlay-div is gone, not wait 15 seconds for that.
How do I check that this overlay is gone and then immediately continue?
You could potentially try using "Wait Until Keyword Succeeds" allowing yourself a custom retry timeout. Especially useful if you want to ensure the warning is removed from display in an appropriate amount of time.
Wait Until Keyword Succeeds ${retry} ${retry-interval} Element Should Not Be Visible ${warning-overlay-div}
${retry} = the amount of time overall to perform the check
${retry-interval} = the time between each retry
Time Formats
(I would have made this just a comment but I cannot until I have 50rep - this is just something that might be worth trying)

Selenium + Chrome Driver drag and drop not working properly

I am using Selenium 3.14 and ChromeDriver 2.42 with Chrome 69.0. I was always using this code to drag and drop, which worked until recently, when tests started failing:
$driver->action()->clickAndHold()->moveByOffset(100, 0)->release()->perform();
After some Chrome update, it just stopped dragging. I also tried dragAndDropBy() function, with no luck. (dragAndDrop() is not an option because I need to move inside one element).
After hours of experimenting I was able to narrow the issue down to this weird thing, when using moveByOffset with bigger number, like 100 in my example fails, it just weirdly bounces in place. But when I tried to move it only by couple of px, it worked.
This is the code I ended up with:
$driver->action()->clickAndHold()->perform();
for($i = 0; $i < 10; $i++){
//moving this 10 times by 10 px instead of once by 100px
$driver->action()->moveByOffset(10, 0)->perform();
}
sleep(1);
$driver->action()->release()->perform();
I just want to share this, if somebody encountered similar issue.

Selenium SelectWindow Command not working

Selenium Select Window command fails and shows "Could not find window with title....". but if i Execute the Select Window command alone it passes the case and verifying the elements.
Code i used:
public void testDefaultlogo() throws Exception {
selenium.open("http://Sitename/samp.aspx");
selenium.type("ctl00_mainContentPlaceHolder_txt_LoginName", "uname");
selenium.type("ctl00_mainContentPlaceHolder_txt_Password", "pwd#12");
selenium.click("ctl00_mainContentPlaceHolder_btn_login");
selenium.waitForPageToLoad("60000");
selenium.click("ctl00_defaultLogo");
selenium.selectWindow("Sample~Window-ID");
verifyEquals("http://Sitename/index.html", selenium.getLocation());
selenium.close();
selenium.selectWindow ("null");
verifyTrue(selenium.isElementPresent("ctl00_defaultLogo"));
I mean by clicking one by one of the follwing commands in Selenium IDE it shows green but if i run the case it failed and shows as i mentioned above
What I can get from your code is that, clicking the "ctl00_defaultLogo" will popup another window. The possible issue could be that after write the command selenium.click("ctl00_defaultLogo") you need to wait for the popup to load.
The possible resolution, insert the command
selenium.WaitForPopUp("Sample~Window-ID", "5000");
before the
selenium.selectWindow("Sample~Window-ID");
This will cause selenium to wait for 5 secs before selecting the popup window.
Hope this helps!!!
some times it may not work for that u better use.
open_window("URL","WindowID or title or name");
selenium.selectWindow("WindowID or title or name");