无法通过Selenium和Java在https://spicejet.com中选择出发日期

时间:2018-10-29 05:48:36

标签: java selenium selenium-webdriver xpath webdriverwait

我正在尝试从日历https://spicejet.com/中选择一个截至“ 2018年10月31日”的“离开日期”,但是我收到了错误“未知错误:元素在点(832,242)不可单击。其他元素会收到点击:...”,请帮帮我。这是我的代码得到这样的异常:

public class bookflight extends Thread {

    UtilityMethods utilObj= new UtilityMethods();
    @Test
    public void SighnUp() throws IOException
    {
        utilObj.getdriver().get("https://spicejet.com");
        utilObj.getdriver().manage().window().maximize();

        utilObj.getdriver().findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();
        utilObj.getdriver().findElement(By.xpath("//a[contains(text(),'Guwahati (GAU)')]")).click();
        utilObj.getdriver().findElement(By.xpath("//a[contains(text(),'Goa (GOI)')]")).click();
        utilObj.getdriver().findElement(By.className("ui-datepicker-trigger")).click();
        utilObj.getdriver().findElement(By.xpath("//div[@class='ui-datepicker-group ui-datepicker-group-first'])/parent:://table[@class='ui-datepicker-calendar']following-sibling::./a/contains(text(),'31')")).click();           
    }
}

5 个答案:

答案 0 :(得分:3)

要选择来自(例如 Guwahati(GAU)),(例如 Goa(GOI)), url https://spicejet.com/中的目标位置和 DEPART DATE 作为 31/10 ,您需要为 WebDriverWait 所需的可点击元素,您可以使用以下解决方案:

  • 代码块:

    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import org.openqa.selenium.support.ui.ExpectedConditions;
    import org.openqa.selenium.support.ui.WebDriverWait;
    
    public class spicejet_login {
    
        public static void main(String[] args) {
    
    
            System.setProperty("webdriver.gecko.driver", "C:\\Utility\\BrowserDrivers\\geckodriver.exe");
            WebDriver driver = new FirefoxDriver();
            driver.get("https://spicejet.com");
            new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT']"))).click();
            driver.findElement(By.xpath("//div[@id='glsctl00_mainContent_ddl_originStation1_CTNR']//table[@id='citydropdown']//li/a[@value='GAU']")).click();
            driver.findElement(By.xpath("//div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//table[@id='citydropdown']//li/a[@value='GOI']")).click();
            new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[@class='ui-datepicker-calendar']//tr//a[contains(@class,'ui-state-default') and contains(.,'31')]"))).click();
        }
    }
    
  • 浏览器快照:

spicejet_depart_date_31oct2018.png

答案 1 :(得分:0)

导致此异常的因素很多, 我想建议您尝试等待一下。

WebDriverWait wait = new WebDriverWait(utilObj.getdriver(), 10);
wait.until(ExpectedConditions.elementToBeClickable(By.id("ctl00_mainContent_ddl_originStation1_CTXT")));

然后尝试点击元素

utilObj.getdriver().findElement(By.id("ctl00_mainContent_ddl_originStation1_CTXT")).click();

答案 2 :(得分:0)

根据异常类型,您可以按Action类单击元素:

Actions action = new Actions(driver);
action.moveToElement(WebElement to click).click().perform();

答案 3 :(得分:0)

更新答案以单击下一个日期。

//div[contains(@class, 'ui-datepicker-group-first')]//td[@data-month='9' and @data-year='2018']/a[.=31]

您可以修改以上XPATH来选择基于YEAR/MONTH/DATE的日期。更多XPath creation go-through my answers

var path ="//div[contains(@class, 'ui-datepicker-group-first')]//td[@data-month='9' and @data-year='2018']/a[.=31]";
var elem = document.evaluate(path, window.document, null, 9, null ).singleNodeValue;
console.log( elem );
elem.click();

当您输入FROMTO数据时,DEPART DATE字段将被自动选择。因此,只需要使用javascript选择第一个数据即可。

FROM « //div[@id='ctl00_mainContent_ddl_originStation1_CTNR']//a[@text='Guwahati (GAU)']
TO « //div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[@text='Goa (GOI)']
DEPART DATE «
//div[contains(@class, 'ui-datepicker-group-first')]//a[contains(@class, 'ui-state-active')]

示例测试程序。

import io.github.yash777.driver.Browser;
import io.github.yash777.driver.Drivers;
import io.github.yash777.driver.WebDriverException;

public class SpiceJET {
    static WebDriver driver;
    static WebDriverWait explicitWait;
    public static void main(String[] args) throws WebDriverException, IOException {
        test();
    }
    public static void test() throws WebDriverException, IOException {
        Drivers drivers = new Drivers();
        String driverPath = drivers.getDriverPath(Browser.CHROME, 63, "");

        System.setProperty(ChromeDriverService.CHROME_DRIVER_EXE_PROPERTY, driverPath);

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        driver = new ChromeDriver( capabilities );
        explicitWait = new WebDriverWait(driver, 10);

        //Maximize browser window
        driver.manage().window().maximize();
        //Go to URL which you want to navigate
        driver.get("https://spicejet.com/");

        clickElement("//input[@id='ctl00_mainContent_ddl_originStation1_CTXT'][1]");
        clickElement("//div[@id='ctl00_mainContent_ddl_originStation1_CTNR']//a[@text='Guwahati (GAU)']");
        clickElement("//input[@id='ctl00_mainContent_ddl_destinationStation1_CTXT'][1]");
        clickElement("//div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//a[@text='Goa (GOI)']");

        clickUsingJavaScript("//div[contains(@class, 'ui-datepicker-group-first')]//a[contains(@class, 'ui-state-active')]");
    }
}
public static void clickElement(String locator) {
    By findBy = By.xpath( locator );
    WebElement element = explicitWait.until(ExpectedConditions.elementToBeClickable( findBy ));
    element.click();
}

public static void clickUsingJavaScript( String locator ) {
    StringBuffer click = new StringBuffer();
    click.append("var elem = document.evaluate(\""+locator+"\", window.document, null, 9, null ).singleNodeValue;");
    click.append("elem.click();");
    System.out.println("JavaScript Click.");
    jse.executeScript( click.toString() );
}
  

要在运行时自动管理Selenium Driver Executable for Java,请使用SeleniumWebDrivers

注意::如果您选择的是自动选择的DEPART DATE,那么硒会引发异常

  

线程“主要” org.openqa.selenium.WebDriverException中的异常:未知错误:   元素<input type="text" readonly="readonly" id="ctl00_mainContent_view_date2" class="custom_date_pic required home-date-pick">   在点(784,241)不可点击。其他元素将获得点击:<span class="ui-datepicker-month">...</span>

答案 4 :(得分:0)

我希望下面的代码对您有所帮助,并能处理出发和返回的日期

public class SpicejetDropdowns1 {	
public static void main(String[] args) throws InterruptedException
{	System.setProperty("webdriver.chrome.driver","E:\\ChromeDriver\\ChromeDriver2.46\\chromedriver.exe");
WebDriver driver=new ChromeDriver();   
driver.get("http://www.spicejet.com/");
driver.manage().window().maximize();
System.out.println(driver.getTitle());	    driver.findElement(By.cssSelector("#ctl00_mainContent_rbtnl_Trip_1")).click();
// OriginStation
driver.findElement(By.xpath(".//*[@id='ctl00_mainContent_ddl_originStation1_CTXT']")).click();
driver.findElement(By.cssSelector("a[value='DEL']")).click();     
// Destination
driver.findElement(By.xpath(".//*[@id='ctl00_mainContent_ddl_destinationStation1_CTXT']")).click();
driver.findElement(By.xpath("(//a[@value='HYD'])[2]")).click();
Thread.sleep(3000);	driver.findElement(By.xpath("//input[@id='ctl00_mainContent_view_date1']")).click();
if(driver.findElement(By.id("Div1")).getAttribute("style").contains("1"))
{
System.out.println("its enabled");
Assert.assertTrue(true);
}
else
{
Assert.assertTrue(false);
}
while(!driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText().contains("October"))
{
// driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
driver.findElement(By.xpath("//a[@class='ui-datepicker-next ui-corner-all']/span[@class='ui-icon ui-icon-circle-triangle-e']")).click();
System.out.println(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText());
}
List<WebElement> dates= driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr//td"));
int count= dates.size();
for(int i=0; i<count; i++)
{
String txt = driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr//td")).get(i).getText();
if(txt.equalsIgnoreCase("28"))
{
driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr//td")).get(i).click();
System.out.println(txt);
break;
}
}
// Return Date  Selection
Thread.sleep(3000);	driver.findElement(By.xpath("//input[@id='ctl00_mainContent_view_date2']")).click();
while(!driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText().contains("October"))
{
// driver.findElement(By.xpath("//span[contains(text(),'Next')]")).click();
driver.findElement(By.xpath("//a[@class='ui-datepicker-next ui-corner-all']/span[@class='ui-icon ui-icon-circle-triangle-e']")).click();
System.out.println(driver.findElement(By.cssSelector("div[class='ui-datepicker-title'] [class='ui-datepicker-month']")).getText());
}
List<WebElement> MDates= driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr//td"));
int Mcount= dates.size();
for(int i=0; i<Mcount; i++)
{
String txt = driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr//td")).get(i).getText();
if(txt.equalsIgnoreCase("31"))
{
driver.findElements(By.xpath("//table[@class='ui-datepicker-calendar']//tr//td")).get(i).click();
System.out.println(txt);
break;
}
}
//Select Passengers
Thread.sleep(4000);
driver.findElement(By.xpath(".//*[@id='divpaxinfo']")).click();
Thread.sleep(4000);
WebElement Adults = driver.findElement(By.xpath("//select[@id='ctl00_mainContent_ddl_Adult']")); Select adultsdrp = new Select(Adults);
adultsdrp.selectByValue("2");
WebElement childs = driver.findElement(By.xpath("//select[@id='ctl00_mainContent_ddl_Child']"));
Select childsdrp = new Select(childs);
childsdrp.selectByValue("2");
driver.findElement(By.xpath(".//*[@id='divpaxinfo']")).click();
System.out.println(driver.findElement(By.xpath(".//*[@id='divpaxinfo']")).getText());
//Static Currency Dropdown
WebElement Currency =  driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency"));
Select currencydrp = new Select(Currency);
currencydrp.selectByValue("USD");	Assert.assertEquals(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")).getAttribute("value"), "USD");	System.out.println(driver.findElement(By.id("ctl00_mainContent_DropDownListCurrency")).getAttribute("value"));
}
}