How can selenium click the button via x,y position - selenium

I found the button's x.y position via opencv function matchTemplate(),and now how can I click the button via its x,y position?

In WebDriver, we can use Actions
new Actions(oWebDriver).moveByOffset(100, 200).click().build().perform();
where 100, 200 are x and y axis.

You are 2 ways
Scroll till view and click
JavascriptExecutor js = (JavascriptExecutor) driver;
WebElement elementToClick = driver.findElement(locator values);
jsExec.executeScript("arguments[0].scrollIntoView()", elementToClick);
jsExec.executeScript("arguments[0].click();", elementToClick);
Scroll for a specific limit and click on the element
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollBy(0, 250)", "");
WebElement elementToClick = driver.findElement(locator values);
js.executeScript("arguments[0].click();", elementToClick);

Related

Unable to switch to iframe using Selenium Webdriver

I am trying to learn the feature drag and drop in the webpage
Link
The Section Books is inside the iframe.
But i am unable to access the iframe I am getting below error
no such element: Unable to locate element:
Below are the Xpath i tried
// WebElement frame =driver.findElement(By.xpath("//div[#id='root']//iframe[#class='st-preview-body']"));
// WebElement frame =driver.findElement(By.tagName("iframe"));
WebElement frame =driver.findElement(By.xpath("//iframe[#src='https://snippet.webixcode.com/snippet.html?0.0.3']"));
driver.switchTo().frame(frame);
I also tried using by.id and also xpath using id and class name
What will be the mistake i am doing? To my knowledge there is only one iframe present
The code for drag and drop. will this work?
WebElement fromdrag=driver.findElement(By.xpath("//span[#class='dhx_tree-list-item__text'][normalize-space()='Lawrence Block']"));
WebElement todrop=driver.findElement(By.id("treeTarget"));
Actions act=new Actions(driver);
act.dragAndDrop(fromdrag, todrop).build().perform();
Its a Nested iframe. We need to switch to those iframes one by one to access Elements from All Books.
The page takes time to load, better apply Explicit waits to find the Elements. And you also need to driver.switchTo().defaultContent(); to come out of the iframe and find Elements outside the iframe
public void iframequestion() {
System.setProperty("webdriver.chrome.driver","C:\\expediaproject\\Chromedriver\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
JavascriptExecutor js = (JavascriptExecutor)driver;
driver.get("https://dhtmlx.com/docs/products/dhtmlxTree/");
WebElement frame1 = driver.findElement(By.xpath("//iframe[#src='https://snippet.dhtmlx.com/3e0b5r57?mode=mobile']"));
js.executeScript("arguments[0].scrollIntoView(true);", frame1);
driver.switchTo().frame(frame1);
WebElement frame2 = driver.findElement(By.xpath("//iframe[#class='st-preview-body']"));
driver.switchTo().frame(frame2);
WebElement frame3 = driver.findElement(By.id("content"));
driver.switchTo().frame(frame3);
WebElement ele1 = driver.findElement(By.xpath("//span[text()='Fiction & Fantasy']"));
System.out.println(ele1.getText());
driver.switchTo().defaultContent();
WebElement ele2 = driver.findElement(By.xpath("//a[text()='View more demos']"));
System.out.println(ele2.getText());
driver.quit();
}
Fiction & Fantasy
View more demos

Scrollby method in selenium

I have used the below code for scroll down.
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,1250)", "");
How do you calculate the pixel values when you want to scroll the window to a specific point ?
I guess you are trying to scroll to some element.
what you can do is:
WebElement e = driver.findElement(....);
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("arguments[0].scrollIntoView(true);", e);

How to scroll down the page till bottom(end page) in the Selenium WebDriver

I need to scroll down page till end in the Selenium WebDriver. I tried to scroll down the page by using the following code snippet:
JavascriptExecutor jse6 = (JavascriptExecutor) driver;
jse6.executeScript("window.scrollBy(0,250)", "");
It's being scrolled but I need to scroll down till end page.
We have to use JavascriptExecutor
To scroll using coordinate
((JavascriptExecutor) driver).executeScript("window.scrollBy(0,500)");
To scroll till end of the page
((JavascriptExecutor) driver)
.executeScript("window.scrollTo(0, document.body.scrollHeight)");
To scroll till any element
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollIntoView();", element);
do it with python,
import time
time.sleep(2)
drive.execute_script("window.scrollTo(0, document.body.scrollHeight)")
based on #shubham bansal's
For this you can take the xpath of any object at the end of the page manually.And use the below code.
WebElement lastElement =
driver.findElement(By.xpath("//a[#title='org.apache.spark download']"));
int y = lastElement.getLocation().getY();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,"+y+")");
Thread.sleep(3000);

I want to scroll the page

I want to scroll the page iI used following code but I need to move scrollbar manually up to 1/4th then it get scroll automatically what is the reason behind this, any new code suggestion.
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.className("mCSB_dragger_bar"));
int numberOfPixelsToDragTheScrollbarDown = 1000;
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(10,numberOfPixelsToDragTheScrollbarDown).release().perform();
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(10,numberOfPixelsToDragTheScrollbarDown).release().perform();
I generally use JavascriptExecutor for this purpose.
WebDriver driver = new FirefoxDriver();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," + "document.body.scrollHeight,document.documentElement.clientHeight));");

How to select an element from a menu using Webdriver Selenium ? The Menu drop down shows up on Mouse Over?

How to select an element from a menu using Webdriver Selenium ? The Menu drop down shows up on Mouse Over?
You can check it in two ways:
1) first way is to use actions builder
WebElement mnuElement;
WebElement submnuElement;
mnEle = driver.findElement(By.Id("mnEle")).Click();
sbEle = driver.findElement(By.Id("sbEle")).Click();
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.MoveToElement(mnEle).Perform();
// Giving 5 Secs for submenu to be displayed
Thread.sleep(5000L);
// Clicking on the Hidden SubMenu
driver.findElement(By.Id("sbEle")).Click();
See here
2) another approach is to click directly needed element using jscript without simulating mouse hover event:
String cssLocatorOfTheElement=....//locator of the element to click on
JavascriptExecutor js = (JavascriptExecutor) driver;
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("var x = $(\'"+cssLocatorOfTheElement+"\');");
stringBuilder.append("x.click();");
js.executeScript(stringBuilder.toString());
hope this works for you)
Simulate mouseOver event and then select element you can like that:
var elementToShowMenu = Driver.FindElement(Byl.Id("some id"));
new Actions(Driver).MoveToElement(elementToShowMenu).Release(elementToShowMenu).Build().Perform();
var menuElement = Driver.FindElement(Byl.Id("your menu id"));
Here is how I click a invisible anchor link on a tag: a link that is generated dynamically by Javascript:
public static void mouseClickByLocator( String cssLocator ) {
String locator = cssLocator;
WebElement el = driver.findElement( By.cssSelector( locator ) );
Actions builder = new Actions(driver);
builder.moveToElement( el ).click( el );
builder.perform();
}
WebElement mnuElement;
WebElement submnuElement;
mnuElement = driver.findElement(By.cssSelector("insert selector here"));
submnuElement = driver.findElement(By.cssSelector("insert selector here"));
Actions builder = new Actions(driver);
// Move cursor to the Main Menu Element
builder.moveToElement(mnuElement).perform();
// Pause 2 Seconds for submenu to be displayed
TimeUnit.SECONDS.sleep(2); //Pause 2 seconds
// Clicking on the Hidden submnuElement
submnuElement.click();