I am getting the following error:
ElementClickInterceptedException: element click intercepted: Element is not clickable at point (288, 833)
When I manually scroll down the page to view the element, selenium finds it and clicks on it, but only if I help it by scrolling down. I have tried different methods to resolve this that I have commented out in the code, but it is still not resolved? How do I resolve this error?
package mypackage;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.WebDriverManager;
public class DynamicWebTable {
public static void main(String[] args) throws InterruptedException {
WebDriverManager.chromedriver().setup();
WebDriver driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10));
driver.get("https://demo.opencart.com/admin/");
driver.manage().window().maximize();
WebElement username = driver.findElement(By.id("input-username"));
username.clear();
username.sendKeys("demo");
WebElement password = driver.findElement(By.id("input-password"));
password.clear();
password.sendKeys("demo");
driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
//Close popup
if(driver.findElement(By.xpath("//div[@class='modal-content']")).isDisplayed()) {
driver.findElement(By.xpath("//button[@class='btn-close']")).click();
}
driver.findElement(By.xpath("//a[normalize-space()='Sales']")).click();
driver.findElement(By.xpath("//a[normalize-space()='Orders']")).click();
//get total no of pages
String textWithTotalPages = driver.findElement(By.xpath("//div[@class='col-sm-6 text-end']")).getText();
int pages = getNumberOfPages(textWithTotalPages);
System.out.println(pages);
//
for(int p = 1; p <= 2; p ) {
WebElement active_page = driver.findElement(By.xpath("//ul[@class='pagination']//li//span"));
System.out.println("Active Page: " active_page.getText());
Thread.sleep(3000);
//new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(active_page)).click();
active_page.click();
//Actions actions = new Actions(driver);
//actions.moveToElement(active_page).click().perform();
//JavascriptExecutor e = (JavascriptExecutor)driver;
//e.executeScript("arguments[0].click();", active_page);
//get number of rows
int rows = driver.findElements(By.xpath("//table[@class='table table-bordered table-hover']//tbody/tr")).size();
System.out.println("No of Rows: " rows);
//read rows from each page
for(int r=1; r<=rows; r ) {
String orderId = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr[" r "]//td[2]")).getText();
String store = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr[" r "]//td[3]")).getText();
String customer = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr[" r "]//td[4]")).getText();
String status = driver.findElement(By.xpath("//table[@class='table table-bordered table-hover']//tbody//tr[" r "]//td[5]")).getText();
System.out.println(orderId " " store " " customer " " status);
}
//click next page
String nextPage = Integer.toString(p 1);
driver.findElement(By.xpath("//ul[@class='pagination']//li//a[text()='" nextPage "']")).click();
}
driver.quit();
}
//extract number of pages from String
public static int getNumberOfPages(String text){
return Integer.valueOf(text.substring(text.indexOf("(") 1, text.indexOf("Pages")-1));
}
}
CodePudding user response:
You have to close this pop updriver.findElement(By.xpath("//button[@class='btn-close']")).click
before you execute
driver.findElement(By.xpath("//a[normalize-space()='Sales']")).click();
This will resolve the problem for you. I have tried in WATIR, it's working for me.
Okay, So try writing this code in try catch block.
try{
activePage.click();
}catch(ElementClickInterceptedException e){
}
or scroll down the page and issue the click, it works too
JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
Thread.sleep(2000);
activePage.click();
CodePudding user response:
Try the below code, I made 2 changes to your code:
- Using JavascriptExecutor, scroll down to the bottom of the page and then perform
active_page.click()
- In the second iteration since the DOM structure changes,
active_page.click()
won't work(you may get Stale element reference, Element is not attached to the page document exception), so you need to assign the web element once again
Good luck.
for(int p = 1; p <= 2; p ) {
WebElement active_page = driver.findElement(By.xpath("//ul[@class='pagination']//li//span"));
System.out.println("Active Page: " active_page.getText());
Thread.sleep(3000);
//below 2 lines will scroll to the bottom of the page
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("window.scrollBy(0,document.body.scrollHeight)");
Thread.sleep(3000);
//assigning web element to active_page once again as the DOM structure has changed
active_page = driver.findElement(By.xpath("//ul[@class='pagination']//li//span"));
active_page.click();