java 无法解决的异常错误

7cwmlq89  于 2023-01-29  发布在  Java
关注(0)|答案(2)|浏览(120)

我收到以下错误:
元素点击拦截异常:拦截的元素点击:元素在点(288,833)处不可单击
当我手动向下滚动页面查看元素时,selenium找到并点击了它,但只有当我向下滚动时才能帮助它。我尝试了不同的方法来解决这个问题,我已经在代码中注解掉了,但仍然没有解决?我该如何解决这个错误?

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));
        
    }
    
    
}
jdgnovmf

jdgnovmf1#

您必须关闭此弹出窗口driver.findElement(By.xpath("//button[@class='btn-close']")).click
在执行之前

driver.findElement(By.xpath("//a[normalize-space()='Sales']")).click();

这将为您解决问题。我已经尝试在WATIR,它为我工作。
好的,那么试着在try catch块中写这段代码。

try{
activePage.click();
}catch(ElementClickInterceptedException e){
}

或者向下滚动页面并发出点击,它也可以工作

JavascriptExecutor js= (JavascriptExecutor) driver;
js.executeScript("window.scrollTo(0,document.body.scrollHeight)");
Thread.sleep(2000);
activePage.click();
7xllpg7q

7xllpg7q2#

尝试下面的代码,我做了2更改你的代码:

  • 使用Javascript执行器,向下滚动到页面底部,然后执行active_page.click()
  • 在DOM结构更改后的第二次迭代中,active_page.click()将不起作用(您可能会得到Stale元素引用,Element未附加到页面文档异常),因此需要再次分配web元素

祝你好运。

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();

相关问题