selenium webdriver给出"未知错误:元素不可点击(747,238)"单击角度js日期选择器时

时间:2016-12-22 00:27:07

标签: selenium selenium-webdriver

package redbus;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;

public class Searchforbus {

   public static  void main(String[] args) throws InterruptedException  {
     System.setProperty("webdriver.chrome.driver", "./drivers/chromedriver.exe");
     ChromeDriver driver = new ChromeDriver();
     driver.get("http://www.redbus.in/");
     driver.manage().window().maximize();
     driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
     driver.findElementById("src").sendKeys("Nagercoil");
     driver.findElementByClassName("selected").click();
     driver.findElementById("dest").sendKeys("Chennai");
     driver.findElementByClassName("selected").click();
     Thread.sleep(3000);
     driver.findElementById("onward_cal").click();
     WebElement element1= driver.findElementByXPath("//div[@id='rb-calendar_onward_cal']/table/tbody/tr[3]/td[6]");
     System.out.println("Check1");
     Actions builder= new Actions(driver);
     builder.moveToElement(element1).click().perform();
     System.out.println("Check2");


    }

}

我收到以下错误:

Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: Element is not clickable at point (747, 238). Other element would receive the click: <label for="onward_cal" style="font-family:Lato" class="db text-trans-uc move-up">...</label>
  (Session info: chrome=54.0.2840.99)

at redbus.Searchforbus.main(Searchforbus.java:28)

3 个答案:

答案 0 :(得分:0)

@vinu在locator element1中提供的日期指向12月3日,由于过去的日期已在日历中被禁用,请确保您提供启用/可用日期

养成使用显式等待条件的习惯,然后再点击像日历这样的棘手元素 例如WebDriverWait wait = new WebDriverWait(driver,15);

wait.until(ExpectedConditions.elementToBeClickable(By.xpath( “element_xpath”)));

ref links

答案 1 :(得分:0)

拉胡尔是对的。您选择了已禁用的过去日期。尝试选择未来日期,如果失败,请告诉我们错误是什么。

答案 2 :(得分:0)

import java.awt.Robot;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Set;


import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.NoAlertPresentException;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;


public class CommonSeleniumUtils extends Driver {

     static WebDriver driver = Driver.getDriver();

    public void switchToWindowByTitle(String title) {
        Set<String> windows = driver.getWindowHandles();
        System.out.println("Amount of windows that are currently present :: " + windows.size());
        for (String window : windows) {
            driver.switchTo().window(window);
            if (driver.getTitle().startsWith(title) || driver.getTitle().equalsIgnoreCase(title)) {
                break;
            } else {
                continue;
            }
        }
    }

    public void clickWithJS(WebElement elementtoclick) {
        WebDriverWait wait = new WebDriverWait(driver, Long.parseLong(ConfigurationReader.getProperty("timeout")));
        wait.until(ExpectedConditions.elementToBeClickable(elementtoclick));
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", elementtoclick);
        ((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementtoclick);
    }

    public void waitForPresenceOfElementByCss(String css) {
        WebDriverWait wait = new WebDriverWait(driver, Long.parseLong(ConfigurationReader.getProperty("timeout")));
        wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(css)));
    }

    public void waitForVissibilityOfElement(WebElement element) {
        WebDriverWait wait = new WebDriverWait(driver, Long.parseLong(ConfigurationReader.getProperty("timeout")));
        wait.until(ExpectedConditions.visibilityOf(element));
    }

    public void waitForStaleElement(WebElement element) {
        int i = 0;
        while (i < 10) {
            try {
                element.isDisplayed();
                break;
            } catch (StaleElementReferenceException e) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
                i++;
            } catch (NoSuchElementException e) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
                i++;
            } catch (WebDriverException e) {
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
                e.printStackTrace();
                i++;
            }
        }
    }

    public boolean verifyElementIsNotPresent(String xpath) {
        List<WebElement> elemetns = driver.findElements(By.xpath(xpath));
        return elemetns.size() == 0;
    }

    public boolean verifyElementIsNotPresent(By by) {
        List<WebElement> elemetns = driver.findElements(by);
        return elemetns.size() == 0;
    }

    public void scrollToElement(WebElement element) {
        ((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(true);", element);
    }

    public void hitEnterUsingRobot() {
        Robot rb;
        try {
            rb = new Robot();
            rb.keyPress(KeyEvent.VK_ENTER);
            rb.keyRelease(KeyEvent.VK_ENTER);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    public boolean verifyAlertPresent() {
        try {
            driver.switchTo().alert();
            return true;
        } catch (NoAlertPresentException Ex) {
            System.out.println("Alert is not presenet");
        }
        return false;
    }

    public static void takeSnapShot() {
        try {
            TakesScreenshot scrShot = ((TakesScreenshot) driver);
            File SrcFile = scrShot.getScreenshotAs(OutputType.FILE);
            String path = System.getProperty("user.dir") + "\\screenshots.jpg";
            System.out.println(path);
            File DestFile = new File(path);
            FileUtils.copyFile(SrcFile, DestFile);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }

    }
    public void safeJavaScriptClick(WebElement element) throws Exception {
        try {
            if (element.isEnabled() && element.isDisplayed()) {
                System.out.println("Clicking on element with using java script click");

                ((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
            } else {
                System.out.println("Unable to click on element");
            }
        } catch (StaleElementReferenceException e) {
            System.out.println("Element is not attached to the page document "+ e.getStackTrace());
        } catch (NoSuchElementException e) {
            System.out.println("Element was not found in DOM "+ e.getStackTrace());
        } catch (Exception e) {
            System.out.println("Unable to click on element "+ e.getStackTrace());
        }
    }
}

After this, call this method ===>>> safeJavaScriptClick


     WebElement element =  driver.findElement(By.xpath("YOUR XPATH"));
        try {
            seleniumTools.safeJavaScriptClick(element);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }