Selenium - Scroll to Element - java

Is there any way to scroll page to current webElement using selenium for Java?
I would like to click on element but when the element is down or up it clicks on another place - so I want to scroll page to current element.
I searched but havent found any solution.
Any help - Advance Thanks !

You need look up the id and fire click event on that id.
I want to click on next button on my page whose id="nextButton"
In the page object here is how i declared. That way you dont need worry about where the position is
Eg:
#FindBy(how = How.ID, using = "nextButton")
private WebElement next;
More info at
http://reddymails.blogspot.com/2011/09/selenium-2-or-webdriver-for-automation.html

Related

Java Selenium Get Element for Click

i have some problems to get the right element to perform a click.
I use selenium.
I want to click on the Log In "Button" on this page https://campus.uni-stuttgart.de/cusonline/webnav.ini
Maybe sb could help me.
Thanks
Try this code. The login button is available under a frame. so it is necessary to switch to that frame in order to access the element.
driver.get("https://campus.uni-stuttgart.de/cusonline/webnav.ini");
driver.switchTo().frame(driver.findElement(By.xpath("//*[#name='menue']")));
driver.findElement(By.cssSelector("#menue_frame_key_icon > img")).click();
driver.switchTo().defaultContent();
It appear the login button id is "menue_frame_key_icon".
So this should be:
driver.findElement(By.id("menue_frame_key_icon")).click();
You could alternately try the child element of that id, which can be done multiple ways. This will work though:
driver.findElement(By.cssSelector("#menue_frame_key_icon > img")).click();

Appium not clicking on an element

I'm trying to automate one of my hybrid app using Appium. I'm getting an issue while clicking on the Login button. The error message I'm getting is:
org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (116, 329). Other element would receive the click: <button class="button button-medium button-custom-login " ng-click="login()">...</button>
And I just want to click on the same element i.e. the one mentioned here with attribute ng-click="login().
I've changed the context already to WebView and tried with changing the attribute to Native as well but nothing seems to be working.
The code which I've used to identify this element is below:
List<WebElement> labels = driver.findElementsByTagName("button");
I iterated through all the elements and found that I need to click on number 20 element.
Any help on this would be great. Thanks!
You should try using Actions class as below :-
WebElement element = driver.findEle....
Actions action = new Actions(driver);
action.moveToElement(element).click().perform();
If you have found that you need to click on number 20 element in the list, you could go this way:
int pos = 20;
List labels = driver.findElementsByTagName("button");
labels.get(pos-1).click();

Get ID, Class of Currently Focused Element in Selenium

There are many ways to select or focus an element in Selenium, for example using TAB key we can focus on next element.
But, is there any way in Selenium to get all details of current focused element such as id, class, href, text etc ?
i want to focus on Like, Comment or Share button of a post https://www.facebook.com/pitbull/photos/a.440436327400.230702.95051637400/10153236215477401/?type=3&theater of Facebook page of Pitbull, But nothing works for me, i tried xpath, class, id but unable to Focus on share button. i can focus on share button using Tab key about 161 times but how will i confirm that focused element is "Share" button or somethong else? ;)
Here is my sample code
WebDriver driver = new FirefoxDriver();
driver.get("https://www.facebook.com/");
driver.findElement(By.id("email")).click();
driver.findElement(By.id("email")).sendKeys("myemail#yahoo.com");
driver.switchTo().activeElement().sendKeys(Keys.TAB);
after TAB key, you know focus will be go to Password field from email field, so how can i get id, class or other details of focused element in my selenium code? in my example it is Password field.
You can use
WebElement activeElement = driver.switchTo().activeElement();
String className = activeElement.getAttribute("class");
String id = activeElement.getAttribute("id");

How do I scroll down vertically in a specific div in a web page

I have searched all the forums but I didn't get a correct answer for my issue. My web page to test has a link hidden below, and I am trying to find it manually by searching for it with xpath or the ID attribute of the element, but I am not able to find it when I am running the web driver script. Even when it is not giving any error on that element, I am getting an error on next command/line.
I found below code from the forums, which is scrolling whole page. I don't want this, I want to scroll down vertically in a specific div area as in screen shot.
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("ctl00_Menu1_scrollDiv.scrollBy(0,250)", "");
div id for this is "ctl00_Menu1_scrollDiv"
Element id: ctl00_Menu1_DlMenu_ctl09_LnkMenuname
Please help me on this issue. Thanks in advance.
Help will be appreciated.
This is umendra tomar , I have found very simple solution for this, please use the below code for scroll a specific div in html using selenium .
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("document.getElementById('scrollCustom').scrollTop= 450");
scrollCustom = this is the ID of your div which you want to scroll.
document.getElementById = this is use in javascript to locate a webelement.
Don't worry we can use this in java using javascriptExecutor
Check if this works for you.
var s = $('#ctl00_Menu1_scrollDiv').scrollTop();
This will give the current value of the scroll in the div.Use this only if you want to scroll inside a div to a certain point dynamically. Otherwise you can hardcode the scrollTop value inside animate()
Using the current value of your scroll you can parameterize the given below scrollTop parameter
$("#ctl00_Menu1_scrollDiv").animate({ scrollTop: "100px" }); // Here 100 px is just an example
I had used this to scroll a large div programmatically in my webdriver framework. Also, this will work if your AUT has jQuery loaded in the browser.
In Java:
JavascriptExecutor js;
js = (JavascriptExecutor) driver;
js.executeScript("$(\"#ctl00_Menu1_scrollDiv\").animate({ scrollTop: \"100px\" })");
First you should not just reference an element by the id. You should set scrollTop to scroll it to a position.
document.getElementById("ctl00_Menu1_scrollDiv").scrollTop(250);
Non-JQuery solution based on this post.
((JavascriptExecutor) driver).executeScript(
"arguments[0].scrollTop=arguments[1].offsetTop",
divWithScrollbarElement,
elementToScrollTo);
where divWithScrollbarElement is the div element which you are looking to scroll, and elementToScrollTo is the child element which you want to make viewable (which in my case was actually the parent of the element which I was initially trying to view). If elementToScrollTo is not actually in the DOM yet, you may need to use the script once to scroll down as far as possible, and then again once more elements have loaded.

automate move to next slide of SlideShare using Selenium

My aim is to automate next Slide of SlideShare present on the web page using selenium.
How to next/previous SlideShare with Java code for Selenium?
WebElement slide = getDriver().findElement(By.id("slidesharePlayer")); // use the id of the searchbar to find it
Thread.sleep(2000);
slide.sendKeys("next()");
this fails to move next slide
any idea how to move next slide ?
new Actions(getDriver())
.sendKeys(getDriver().findElement(By.id("slidesharePlayer")), Keys.ARROW_RIGHT)
.build().perform();
by press right key from key bord ...
You are doing it all wrong.
Slideshare doesn't have any element with id slidesharePlayer.
Using Thread.sleep is not a good idea.
sendKeys() will send the keys next() to the element slide.
You can do something like..
getDriver().findElement(By.xpath("//a[contains(#title, 'Next Slide')]").click();
which clicks on an a element with title property set to "Next Slide" .

Categories

Resources