I want to scroll down my web page and i m using this code to scroll page but it is not working
public ViewBasketSentToMePageObject viewSlideShare() throws InterruptedException {
Thread.sleep(500l);
Actions action1 =new Actions(getDriver());
action1.keyDown(Keys.CONTROL).sendKeys(String.valueOf('\u0030')).build().perform();
List<WebElement> function = getDriver().findElements(By.xpath("//a [#ng-click='onItemClick()']"));
function.get(13).findElement(By.xpath("//img [#ng-src='resources/images/slideshare-icon-small.png']")).click();
return getFactory().create(ViewBasketSentToMePageObject.class);
}
Looking for help
Try using simple java script below and you can scroll the page.
JavascriptExecutor jsx = (JavascriptExecutor)driver;
jsx.executeScript("window.scrollBy(0,450)", "");
For scroll down:
WebDriver driver = new FirefoxDriver();
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("scroll(0, 250);");
or, you can do as follows:
jse.executeScript("window.scrollBy(0,250)", "");
Scroll until find the WebElement
Try this:
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", your_WebElement);
WebElement element = driver.findElement(By.xpath("//input [#id='giveid']"));
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView();", element);
Use this. This will help you to scroll down at the particular element. I had tested on my website even. It is working fine.
Related
I'm trying to click on Calendar but everytime I try to click on the calendar the error pop up as "element click intercepted: Element is not clickable at point (293, 1317)
<input type="text" name="form_fields[travel_comp_date]" id="form-field-travel_comp_date"
class="elementor-field elementor-size-sm elementor-field-textual elementor-date-field
flatpickr-input" placeholder="Date of travel"
pattern="[0-9]{4}-[0-9]{2}-[0-9]{2}" readonly="readonly">
Here's my code:
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(5));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//input[#name='form_fields[travel_comp_date]']")));
driver.findElement(By.xpath("//input[#name='form_fields[travel_comp_date]']")).click();
Can someone please help me correcting this.
Please try the following code with explicit wait and let me know if it works for you:
enter code here
driver.get("https://www.path2usa.com/travel-companion/");
WebElement dateTravel =
driver.findElement(By.xpath("//input[#id='form-field-
travel_comp_date']"));
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,1000)");
WebDriverWait wait = new WebDriverWait(driver,
Duration.ofSeconds(5));
js.executeScript("arguments[0].scrollIntoView();", dateTravel);
wait.until(ExpectedConditions.visibilityOf(dateTravel));
if(dateTravel.isDisplayed())
{
js.executeScript("document.getElementById('form-field-
travel_comp_date').value='12/20/2023'");
}
The element you are trying to access is out of the initially presented view port.
In order to click it you first need to scroll the page to bring that element into the visible view port and only after that you will be able to click on it.
Please try this:
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollTo(0, document.body.scrollHeight)");
Or this
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,600)");
After that try performing your code
I am trying to click a button in selenium java code and it is not clicking all the time. Apparently this is pretty common issue.
I tried below few solutions:
HTML Code :
<button class="btn btn--action btn--border-white btn--my__calculate" style="display: inline-block;">Final Figure</button>
Solution 1:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
if (btnWorkout.isDisplayed() && btnWorkout.isEnabled()) {
btnWorkout.click();
}
Solution 2 :
WebDriverWait wait = new WebDriverWait(webDriver, 10);
WebElement btnWorkout = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector(".btn--my__calculate")));
btnWorkout.click();
Solution 3:
WebElement btnWorkout = webDriver.findElement(By.cssSelector(".btn--my__calculate"));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
None of them worked for me.
Other strange thing is above step passes without an error and button doesn't click as expected
Induce WebDriverWait And Following Xapth.
Try following options.
Option1:
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
btnWorkout.click();
Option2: Use Action class
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
Actions action=new Actions(webDriver);
action.moveToElement(btnWorkout).click().build().perform();
Option3: Use JavaScript Executor
WebDriverWait wait = new WebDriverWait(webDriver, 20);
WebElement btnWorkout=wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='btn btn--action btn--border-white btn--my__calculate'][text()='Final Figure']")));
JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", btnWorkout);
Curious that the executeScript() option didn't work for you. Keep using your WebDriverWait from option 2, but instead of the click() method, try using Action Chains. I'm not sure how to write this in Java, but it'll be something like this:
Actions action = new Actions(driver);
action.move_to_element(btnWorkout).click(btnWorkout).perform();
Maybe you have another hidden element with same CSS (class). You can try capture the element by linkText or partialLinkText like this:
WebElement btnWorkout = webDriver.findElement(By.linkText("Final Figure"));
I'm trying to click in some JQUERY elements from a very known website to practice Selenium (http://the-internet.herokuapp.com/jqueryui/menu).
I figured out how to navigate into the menu (not sure if my code is a good solution), however am not being able to click in each last submenu option (PDF, CSV, Excel)
I'm trying something like below:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
String jQuerySelector = "$('a#ui-id-6.ui-corner-all')";
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").get(0);");
//click() also did not work
WebElement webElement = (WebElement) ((JavascriptExecutor) driver).executeScript("return $(" + jQuerySelector+ ").click();");
Your JavaScript function of click is wrong.
Use below javascript syntax
executor.executeScript("arguments[0].click();", WebElement);
Below code works for me:
Actions builder = new Actions(driver);
Action mouseOverMenu;
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-2"))).build();
mouseOverMenu.perform(); //accessing Enabled menu option
WebDriverWait wait = new WebDriverWait(driver, 30);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("ui-id-4")));
wait.until(ExpectedConditions.elementToBeClickable(By.id("ui-id-4")));
mouseOverMenu = builder.moveToElement(driver.findElement(By.id("ui-id-4"))).build();
mouseOverMenu.perform(); //accessing Downloads submenu option
WebElement webElement2= driver.findElement(By.cssSelector("a#ui-id-6.ui-corner-all")); // #ui-id-6 is for pdf, #ui-id-7 csv so on
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", webElement2);
This is the html code for the button in the pop up ( pop up has a lead gen form) -
<button id="getCoupon" class="fetch" data-bind="click: submitForm" type="submit">Fetch Coupon</button>
This is the script which I have written in JAVA in Eclipse. I am able to fill the name, Email and Phone number but I'm not able to click on the button -
driver.findElement(By.id("getCoupon")).click();
As per the comment and URL, you have shared :
You can try with this code :
public class Pawan {
static WebDriver driver ;
public static void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\user**\\Downloads\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("http://vets.cm.qa.preview.vca.webstagesite.com/free-first-exam");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("input[value='/santa-monica']~div.select-btn"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.id("fName"))).sendKeys("Pawan");
wait.until(ExpectedConditions.elementToBeClickable(By.id("lName"))).sendKeys("Sharma");
wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("ps12#gmail.com");
wait.until(ExpectedConditions.elementToBeClickable(By.id("zip"))).sendKeys("90404");
wait.until(ExpectedConditions.elementToBeClickable(By.id("phone"))).sendKeys("9697989900");
JavascriptExecutor jse = (JavascriptExecutor)driver;
jse.executeScript("window.scrollBy(0,100)", "");
wait.until(ExpectedConditions.elementToBeClickable(By.id("getCoupon"))).click();
}
}
As per your HTML code, your id is : "getCoupon",
whereas in code you mentioned id as : "getCouponFetch". Please correct and it should work. Code -
driver.findElement(By.id("getCoupon")).click();
If selenium click don't work, use below Java Script click code :
WebElement element = driver.findElement(By.id("getCoupon")); JavascriptExecutor executor = (JavascriptExecutor)driver; executor.executeScript("arguments[0].click();", element);
Firstly, what kind of error did you get??
Try using implicit wait if your getting "NoSuchElementException" like below:
driver.manage().timeOuts().implicitlywait(30,TimeUnit.SECONDS);
Then try using the below way to locate that button:
driver.findElementByXpath("text()[contains(.,'Fetch Coupon')]").click();
As per the HTML you have shared to click() on the button you have to induce WebDriverWait for the desired element to be clickable and you can use either of the following solutions:
cssSelector:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.fetch#getCoupon"))).click();
xpath:
new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='fetch' and #id='getCoupon'][contains(.,'Fetch Coupon')]"))).click();
Update
As an alternative you can use the executeScript() method to invoke the click() as follows:
Using cssSelector:
WebElement fetch_coupon = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("button.fetch#getCoupon")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", fetch_coupon);
Using xpath:
WebElement fetch_coupon = new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//button[#class='fetch' and #id='getCoupon'][contains(.,'Fetch Coupon')]")));
((JavascriptExecutor)driver).executeScript("arguments[0].click();", fetch_coupon);
I need to click on a button 'NEW'. The element button is visible on DOM but it's not clickable because it's overlapped and i need to scroll down left side of the page to make it clickable. I was trying inject some javascript but it didn't help in my case:
JavascriptExecutor js = ((JavascriptExecutor) driver);
js.executeScript("scroll(" + driver.findElement(By.xpath(//div[#class = 'save-new'])).getLocation().getX() + "," + driver.findElement(By.xpath(//div[#class = 'save-new'])).getLocation().getY() + ")");
As i feel #damian should worked but you can also tried my code I used it so mny times
Use this code:
WebElement element = driver.findElement(By.xpath("Value"));
((JavascriptExecutor)driver).executeScript(“arguments[0].scrollIntoView();”, element);
element.click();
Try:
targetElement = driver.findElement(By.xpath("your xpath"));
JavascriptExecutor js = ((JavascriptExecutor) driver);
// This:
js.executeScript("arguments[0].scrollIntoView(true);", targetElement);
targetElement.click();
// Or maybe even just:
js.executeScript("arguments[0].click();", targetElement);
You can try this way:--
JavascriptExecutor js = ((JavascriptExecutor) driver);
//Scroll your page to down using below code
((JavascriptExecutor)driver).executeScript(“window.scroll(100,2000)”);
// click on button
driver.findElement(By.xpath(//div[#class ='save-new'])).click()
Hope this help you :)