First post ...
I discovered javascript and selenium, I'm trying to make a left click of a duration of 1 or 2 seconds.
It is easy to make a right click or a double click, but how to make a long click?
Thank you for your support.
Double click is ok :
Actions action = new Actions(driver);
WebElement link = driver.findElement(By.ID ("Element ID"));
action. doubleClick (link).perform();
Click with executeScript ok:
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("document.querySelector(script).click();",Arguments);
At this point I have no track for the long click ...
You can try this below option
public void loingClick(WebDriver driver,WebElement element, int numberOfSeconds) throws Exception
{
Actions action = new Actions(driver);
action.clickAndHold(element).build().perform();
Thread.sleep(1000*numberOfSeconds);
action.moveToElement(element).release();
}
I have one button section product, but it disables when hover mouse on section product then the button is enabled. I don't know write script do enable a button when Selenium WebDriver finds it.
#Test(priority = 4)
public void TestSelectedItem() {
driver.findElement(By.xpath("/html/body/app-root/ng-component/div/product-list/div/div/div/div/div[2]/div/div[3]/div/div[1]/a")).click();
Select drpItem = new Select(driver.findElement(By.id("pa_colors")));
drpItem.selectByIndex(2);
driver.findElement(By.className("add_to_cart_button")).click();
}
Please help me.
I will give you code in Python which do hover action on the element:
hover_company = driver.find_element_by_xpath("your XPath")
driver.execute_script("arguments[0].scrollIntoView(true);", hover_company)
hover = ActionChains(driver).move_to_element(hover_company)
hover.perform()
First two lines scroll to your element, last two lines -- do hover action on the element.
After this action -- you can click on the button. Try on.
Code in Java:
hover_company = driver.findElement(By.xpath("your XPath"));
driver.executeScript("arguments[0].scrollIntoView(true);", hover_company);
Actions hover = new Actions(driver);
hover.moveToElement(hover_company).perform();
I have a List containing 5 'Buy' buttons. Selenium WebDriver is not clicking the first 'Buy' button but clicks the remaining 4 just fine. Ran a println and it shows all 5 buy buttons being found by WebDriver.
I have tried switching the order of buttons in the List, I have attempted to debug it, I have included explicit wait also but nothing is helping. Anyone has any clues?
My code is as follows:
for (int i = 0; i < allProductsOnsite.size(); i ++) {
//System.out.println(allProductsOnsite.get(i).getText());
if (prodsToBuy.contains(allProductsOnsite.get(i).getText())) {
System.out.println("Found -------" + allProductsOnsite.get(i).getText() );
WebDriverWait wt = new WebDriverWait(driver, 15);
wt.until(ExpectedConditions.visibilityOfAllElements(allProductsOnsite));
allBuyButtons.get(i).click();
}
}
Try to click using JavascriptExecutor
WebElement element= driver.findElement(By.xpath(".//*[#id='loginButton']/div"));
JavascriptExecutor executor = (JavascriptExecutor) driver;
executor.executeScript("arguments[0].click();", element);
Also use some wait if needed before this code
Hope this will help you :)
I am working on Walmart automation using Selenium WebDriver. I have written a function to hover over the Departments Menu "Home , Furniture & Patio" so that it gets highlighted and I can click on "Appliances" link. Here is the function that I have written but it does not seem to hover over the element.
public void NavigateDepartments(){
WebElement ApplianceLink = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions myMouse = new Actions(driver);
myMouse.moveToElement(ApplianceLink).build().perform();
ApplianceLink.click();
}
I also tried giving absolute path for
Xpath("/html/body/div/div/div[3]/div/div/div/ul/li[3]/div/div") to find the element and it did not work either. Am I missing anything ?
You should first hover on the main menu, then move to the new element
WebElement menu = driver.findElement(By.xpath("//path to *appliance*"));
WebElement parentMenu = driver.findElement(By.xpath("//*[div='Home, Furniture & Patio']"));
Actions builder = new Actions(driver);
builder.moveToElement(parentMenu).moveToElement(menu).click().build().perform();
I am using Selenium WebDriver to automate my browser tests. My browser header is floating and is always present irrespective of the browser scroll.
So when I click on certain elements that are present below the current visible region of the browser, selenium tries to scroll the element into view and click them.
But because of the auto scrolling as such the elements are scrolled behind the floating header and when any action is performed on them, the elements in the page header get clicked.
is there any way to limit the default scroll of the WebDriver?
Locatable hoverItem = (Locatable) driver.findElement(By.xpath("//li[text()='Reklama w Google']"));
int y = hoverItem.getCoordinates().getLocationOnScreen().getY();
((JavascriptExecutor)driver).executeScript("window.scrollBy(0,"+y+");");
If you want to scroll on the firefox window using selenium webdriver, one of the way is to use javaScript in the java code, The javeScript code to scroll down is as follows:
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("window.scrollTo(0,Math.max(document.documentElement.scrollHeight," +
"document.body.scrollHeight,document.documentElement.clientHeight));");
You can scroll to the necessary location using javascript You need to use the scrollTo method rather than the scrollBy method for it to work.
public void scrollToElement(By by) {
Locatable element = (Locatable) selenium.findElement(by);
Point p= element.getCoordinates().getLocationOnScreen();
JavascriptExecutor js = (JavascriptExecutor) selenium;
js.executeScript("window.scrollTo(" + p.getX() + "," + (p.getY()+150) + ");");
}
Scroll to top can be done:
private void scrollToTop() {
JavascriptExecutor js = (JavascriptExecutor) webDriver;
js.executeScript("window.scrollTo(0, 0);");
}
Simple use the
.sendKeys(Keys.PAGE_DOWN);
when your element was visible, just click on it, by .click(element).perform();
for me work something like this:
clicker = new Actions(driver);
clicker.sendKeys(Keys.PAGE_DOWN);
Thread.sleep(1000);
clicker.click(button).perform();
Thread.sleep(1000);
For scrolling down:
System.setProperty("webdriver.chrome.driver",
"/home/shreetesh/chromedriver");
WebDriver driver = new ChromeDriver();
String url = "https://en.wikipedia.org/wiki/Main_Page";
driver.get(url);
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scroll(0, 25000);");
To scrolling up just replace the value of scroll with (2500, 0).
Use below code for scrolling up and scrolling down
Actions dragger = new Actions(driver);
WebElement draggablePartOfScrollbar = driver.findElement(By.xpath("<Scroll bar Element >"));
// drag downwards
int numberOfPixelsToDragTheScrollbarDown = 50;
for (int i=10 ; i<500 ; i=i+numberOfPixelsToDragTheScrollbarDown) {
try {
// this causes a gradual drag of the scroll bar, 10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
} catch(Exception e1){}
}
// now drag opposite way (downwards)
numberOfPixelsToDragTheScrollbarDown = -50;
for (int i=500;i>10;i=i+numberOfPixelsToDragTheScrollbarDown){
// this causes a gradual drag of the scroll bar, -10 units at a time
dragger.moveToElement(draggablePartOfScrollbar).clickAndHold().moveByOffset(0,numberOfPixelsToDragTheScrollbarDown).release().perform();
Thread.sleep(1000L);
}
I recently had this problem due to a Drupal menu blocking the element when I ran this code:
public void scrollTo(WebElement x) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", x);
}
After referencing this page, I updated to set the boolean to false using this code, and it works great:
public void scrollTo(WebElement x) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(false);", x);
}