how we perform scroll in table - java

I want to perform scroll down in web table but its not performing with a logic
JavascriptExecutor jse1 = (JavascriptExecutor) driver;
jse1.executeScript("window.scrollBy(0,200)");

Use EventFiringWebDriver.
Steps:
Launch the Web Application.
Locate the WebTable.
Use executeScript.
Below is the code:
eventFiringWebDriver eventFiringWebDriver = new EventFiringWebDriver(driver);
eventFiringWebDriver.executeScript("document.querySelector('.ui-grid-viewport.ng-isolate-scope').scrollTop=6000");

You can make use of Selenium's Actions class.
Steps:
Create your WebDriver instance
WebDriver driver = new ChromeDriver();
Create your Actions instance by passing the driver instance as parameter.
Actions actions = new Actions(driver)
Locate your element (table you want to scroll to)
WebElement element = driver.findElement(By.xpath("tableXpath"));
Move to the element
actions.moveToElement(element).perform();

Related

java driver in selenium project

I am using simple project, where you fill in the form. The last part is to click on a element with no id and no text.
I found that i could use java to do it, but how do i initiate the driver in selenium?
Actions builder = new Actions(driver);
builder.moveToElement(knownElement, 10, 25).click().build().perform();
I tried
WebDriver driver = New ChromeDriver();
but that only raised a lot new problems. I am using IntelliJ.
You need chromedriver.exe to initiate selenium web driver.
First download the ChromeDriver. You need to download ChromeDriver for your respective Operating system from this link
//Then set the download location.
System.setProperty("webdriver.chrome.driver", "C://Selenium-java//chromedriver_win32//chromedriver.exe");
//Creating an object of ChromeDriver
WebDriver driver = new ChromeDriver();
//launching the specified URL
driver.get("https://www.google.com/");
Then find your element to be clicked using xpath.
If you say you don't have any id or text for click then use any parent element as reference which you can identify then use xpath axes to identify it.
Then you can click on that element directly.
driver.findElement(By.xpath("//form[#id = 'id']/child::button")).click;

Unable to traverse through a frame using web driver java

I am trying to get the elements inside the second frame here
but I am getting the error that the element does not exist or the
Unable to locate element: {"method":"xpath","selector":"//frameset/frame[2]//a"}
I have tried every method out there but it does not work for me, this is my code
WebDriver driver = new ChromeDriver();
driver.get("https://dps.psx.com.pk/");
//switch to the mainFrame
WebElement mainFrame = driver.findElement(By.xpath("//frameset/frame[2]//a"));
driver.switchTo().frame(mainFrame);
List<WebElement> childs = mainFrame.findElements(By.xpath(".//*"));
for(WebElement child : childs) {
System.out.println(child);
}
I have also tried waiting for the elements to load and then tried to access the elements inside the frame but still same errors.
The frame locator is //frameset/frame[2], the //a is a drill down into the frame.
You can also use the name attribute directly to switch. switchTo().frame() can receive id, name or WebElement as parameter
driver.switchTo().frame("mainFrame");
If the frame takes time to load you can use explicit wait and ExpectedCondition frameToBeAvailableAndSwitchToIt
WebDriverWait wait = new WebDriverWait(WebDriverRefrence,20);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frameset/frame[2]")));
As per the HTML you have shared to traverse to the desired frame you have to:
Induce WebDriverWait for the desired frame to be available and switch to it.
Induce WebDriverWait for the desired element to be clickable and you can use the following solution:
Using name:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.name("mainFrame")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.name("element_name"))).click();
Using xpath:
new WebDriverWait(driver, 10).until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(By.xpath("//frame[#name='mainFrame' and contains(#src,'index1')]")));
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("element_xpath"))).click();

Click is happening some other place on Cart ICON in MSITE from appium code - Chrome browser

I am trying to click on cart icon on top right corner from Appium in chrome browser mobile.
Code to click :
driver.findElement(By.xpath("//a[#href='/viewcart']")).click();
URL : https://www.2gud.com/?cmpid=2G108229
Note: Please open this URL in mobile device and verify.
Error : Code is clicking somewhere else on mobile device.
This is working. Checked in Android 7.1 emulator
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
public class Demo {
public static WebDriver driver = null;
public static void main(String args[]) throws InterruptedException {
System.out.println("Launching the chrome driver ");
System.setProperty("webdriver.chrome.driver","src\\test\\resources\\drivers\\chromedriver40.exe");
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("androidPackage", "com.android.chrome");
driver = new ChromeDriver(options);
driver.manage().timeouts().pageLoadTimeout(20,TimeUnit.SECONDS);
driver.get("https://www.2gud.com/?cmpid=2G108229");
Thread.sleep(3000);
driver.findElement(By.xpath("//a[#href='/rv/viewcart']")).click();
Thread.sleep(3000);
System.out.println(driver.getTitle());
driver.quit();
}
Try to click using mouse actions or JavaScript executor.
Try using the following code.
Xpath to find the view cart: //a[contains(#href,'viewcart')]
Executing a click via JavaScript has some behaviors of which you should be aware. If, for example, the code bound to the onclick event of your element invokes window.alert(), you may find your Selenium code hanging, depending on the implementation of the browser driver. That said, you can use the JavascriptExecutor class to do this. My solution differs from others proposed, however, in that you can still use the WebDriver methods for locating the elements.
// Assume driver is a valid WebDriver instance that has been properly instantiated elsewhere.
WebElement viewCart = driver.findElement(By.xpath("//a[contains(#href,'viewcart')]"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", viewCart);
You should also note that you might be better off using the click() method of the WebElement interface, but disabling native events before instantiating your driver. This would accomplish the same goal (with the same potential limitations), but not force you to write and maintain your own JavaScript.

Mouse clickAndHold() not working properly on Firefox Using Selenium Webdriver

I am using selenium webdriver client 2.39 and Firefox 26.
Mouse click and hold event does not work properly. My code is like
WebDriver driver=new FirefoxDriver();
driver.get("http://startingwithseleniumwebdriver.blogspot.com/2013/12/frmset1.html");
WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).
clickAndHold(dropdownlists.get(6)).click().build();
This code does not give any error but select only one element.
I can bypass this issue using other way but I want to know whay it is not working.
I face the same problem but it select the element from start to last and give some Error like
Cannot perform native interaction: Could not get node for element - cannot interact
I got the solution by this way you can do this for your problem
builder.clickAndHold(dropdownlists.get(0)).moveToElement(dropdownlists.get(6)).release().build().perform();
If you want to select multiple option from your list try this (it will select first 3 elements):
List<WebElement> elements = driver.findElements(By.xpath("//select[#name='multiselectdropdown']/option"));
for(int i = 0; i < 3; i++) {
new Actions(driver).keyDown(Keys.CONTROL).click(elements.get(i)).keyUp(Keys.CONTROL).perform();
}
ButtonUp (or release()) should be the next button-action following a ButtonDown (or clickAndHold()) button-action (see Appium notes for ButtonDown documentation). Your code performs two consecutive clickAndHolds() followed by a click() without performing a release(). It should be something like:
WebDriver driver=new FirefoxDriver();
driver.get("http://startingwithseleniumwebdriver.blogspot.com/2013/12/frmset1.html");
WebElement multiSelectDropDown=driver.findElement(By.name("multiselectdropdown"));
List<WebElement> dropdownlists = multiSelectDropDown.findElements(By.tagName("option"));
Actions builder=new Actions(driver);
builder.clickAndHold(dropdownlists.get(0)).moveTo(dropdownlists.get(6)).release().build();
While the linked documentation is not for Selenium, Appium is built on top of Selenium.

not able to select hidden link - selenium

I have to select web link when i mouse hover to particular frame in the webpage, the button(link to next page) will be visible.
WebElement mainElement = driver.findElement(By.xpath(<frame xpath >));
Actions builder = new Actions(driver);
builder.moveToElement(mainElement);
WebElement button1 = driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a"));
builder.moveToElement(button1).click().perform();
I am still unable to select the particular link
when i execute, the following error am getting
org.openqa.selenium.ElementNotVisibleException: Element is not currently visible and so may not be interacted with (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 131 milliseconds
But when i hover mouse pointer to the particular frame during AUT(just to move to particular frame without clicking anything), then test is executing sucessfully.
I know this can be handled by JS. But i want to find out is there any solution within selenium webdriver
On some of the drivers for the various browsers, sometimes custom actions like this won't work unless you explicitly enable native events at the time you create the driver:
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
driver = new FirefoxDriver(profile);
or another method of setting it at the time you create DesiredCapabilities for a remote driver
DesiredCapabilities desiredCapabilities = DesiredCapabilities.internetExplorer();
desiredCapabilities.setCapability("nativeEvents", true);
When using Action Chains I have always found it more reliable to perform all the actions in the same chain. As such, rather than 'pausing' to find the now revealed element, try doing so within the chain.
WebElement mainElement = driver.findElement(By.xpath(<frame xpath >));
Actions builder = new Actions(driver);
builder.moveToElement(mainElement).moveToElement(driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a"))).click().perform();
Hopefully that will help.
FirefoxProfile profile = new FirefoxProfile();
profile.setEnableNativeEvents(true);
driver = new FirefoxDriver(profile);
WebElement searchBtn = driver.findElement(By.xpath(""));
WebElement searchBtn1 = driver.findElement(By.xpath(""));
Actions action = new Actions(driver);
action.moveToElement(searchBtn).moveToElement(searchBtn1).click().build().perform();
My View here is Need to Perform Mouse Over on the Object to make it visible and then click on that element.
WebElement mainElement = driver.findElement(By.xpath(<frame xpath >));
Actions builder = new Actions(driver);
builder.moveToElement(mainElement).moveToElement(driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a"))).build().perform();
driver.findElement(By.xpath("//*[#id='currentSkills']/div[1]/div/a")).click();
Please Let me know is the above scripting is working or not.

Categories