I want to scroll the page - selenium

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

Related

How can selenium click the button via x,y position

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

while automating the slider, The slider moves only when i placed my mouse over it in selenium

http://automationpractice.com/index.php?id_category=5&controller=category#/
This is my code,whats wrong with it?
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all.ui-state-hover");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,-40,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();
Where's actions taking under consideration the driver.
Try to enter the following code,
Actions action=new Actions(WebDriver);
You have used a wrong cssSelector. I have modified your code and it should work now.
Here is the java code
System.setProperty("webdriver.chrome.driver", "src/chromedriver 3");
WebDriver driver = new ChromeDriver();
driver.get("http://automationpractice.com/index.php?id_category=5&controller=category#/");
Actions action = new Actions(driver);
WebDriverWait wait = new WebDriverWait(driver, 30);
//modified the css selector
By locator = By.cssSelector(".ui-slider-handle.ui-state-default.ui-corner-all");
wait.until(ExpectedConditions.presenceOfElementLocated(locator));
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
WebElement sliderRight = driver.findElement(locator);
action.dragAndDropBy(sliderRight,70,0).build().perform();
action.moveToElement(sliderRight).click().build().perform();

Popup scroll not working in selenium webdriver

I am running my script in mozilla firefox I want to scroll popup I
applied so many methods but doesn't work for me
I used keys.tab to reach that element but it was unable to enter text in that textfield using senkeys("xyz#gmail.com)
I used scroll method
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.getElementById('youama-email').scrollIntoView(true);");
some exception occur
3.I used Moveto element method but got exception
WebElement element = driver.findElement(By.id("youama-email"));
Actions
actions = new Actions(driver);
actions.moveToElement(element);
actions.click();
actions.perform();
// Initialize Javascript executor
JavascriptExecutor js = (JavascriptExecutor) driver;
// Scroll inside web element vertically (e.g. 100 pixel)
js.executeScript("arguments[0].scrollTop =
arguments[1];",driver.findElement(By.id("<div-id>")), 100);
Please help me to scroll and enter into the email as well as other
fields that will appear after scroll [![enter image description
here][1]][1]
[1]: http://i.stack.imgur.com/D0hqI.png
Try this code. I think what you didn't do was to wait for the element to be visible which i did. See the below code. It is running correctly.
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("http://wyomingvapor.com/");
driver.findElement(By.xpath(".//*[#id='header']/div/div[2]/div/a[1]")).click();
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='y-to-register']/input")));
driver.findElement(By.xpath(".//*[#id='y-to-register']/input")).click();
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath(".//*[#id='youama-firstname']")));
driver.findElement(By.xpath(".//*[#id='youama-firstname']")).sendKeys("xyz#gmail.com");
Thread.sleep(2000L);
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_TAB);
robot.keyRelease(KeyEvent.VK_TAB);
robot.keyPress(KeyEvent.VK_X);
robot.keyRelease(KeyEvent.VK_X);
robot.keyPress(KeyEvent.VK_Y);
robot.keyRelease(KeyEvent.VK_Y);
robot.keyPress(KeyEvent.VK_Z);
robot.keyRelease(KeyEvent.VK_Z);
If you still stuck then do reply to me, Jyotsana.
Happy Learning :-)

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

Select a text and perform a click action

I'd like to select some text and perform a click action - like in Winword where we click Bold after selecting some text...
I have to select the text and click on the <B> bold icon in the textarea.
Any idea on how to do this using Selenium/Webdriver?
In Java, The Advanced User Interactions API has your answer.
// the element containing the text
WebElement element = driver.findElement(By.id("text"));
// assuming driver is a well behaving WebDriver
Actions actions = new Actions(driver);
// and some variation of this:
actions.moveToElement(element, 10, 5)
.clickAndHold()
.moveByOffset(30, 0)
.release()
.perform();
I tried with Action builder and played with offset. It worked for me.
Actions action = new Actions(driver);
action.moveToElement(wblmt,3,3).click().keyDown(Keys.SHIFT).moveToElement(wblmt,200, 0).click().keyUp(Keys.SHIFT).build().perform();
I tried this way and it did not work. Here are the codes:
System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.vn");
driver.manage().window().maximize();
WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
Actions actions = new Actions(driver);
actions.moveToElement(text, 10, 5).clickAndHold().moveByOffset(30, 0).release().perform();
I switched to JavascriptExecutor and it worked:
System.setProperty("webdriver.chrome.driver", "D:/chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.google.com.vn");
driver.manage().window().maximize();
WebElement text = driver.findElement(By.xpath("//*[contains(text(),'Google.com.vn')]"));
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("arguments[0].setAttribute('style', 'background: blue;');", text);