如何在Spicejet网站上单击RoundTrip单选按钮它被选中,但几秒钟后单选为默认

问题描述

如何在SpiceJet网站上单击往返单选按钮。它被选中,但几秒钟后认选择为单向

以下是自2020年11月11日起自动运行spicejet网站的代码。即使我单击“往返”单选按钮,认情况下也会选中单向单选按钮。我该如何解决这个问题?

package practice;

import java.util.List;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.webdriverwait;

public class Spicejet {

    public static void main(String[] args) throws InterruptedException {

        System.setProperty("webdriver.chrome.driver","C:\\Users\\User\\Desktop\\selenium\\chromedriver_win32 (1)\\chromedriver.exe");
        WebDriver driver = new ChromeDriver();
        driver.get("https://www.spicejet.com/");
        driver.manage().window().maximize();
        driver.findElement(By.xpath("//input[@value='roundtrip']")).click();
        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='MAA']"))
                .click();
        driver.findElement(By.xpath(
                "//div[@id='ctl00_mainContent_ddl_destinationStation1_CTNR']//table[@id='citydropdown']//li/a[@value='BLR']"))
                .click();
        driver.close();
    }
}

解决方法

似乎SpiceJet Web应用程序需要更长的时间才能达到document.readyState作为 complete

因此,要在与文本往返行程相关联的click(),您需要为elementToBeClickable()引入WebDriverWait,然后可以使用以下任一Locator Strategies

  • cssSelector

    new WebDriverWait(driver,30).until(webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
    new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.cssSelector("table.tblTrip input[value='RoundTrip']"))).click();
    
  • xpath

    new WebDriverWait(driver,20).until(ExpectedConditions.elementToBeClickable(By.xpath("//table[@class='tblTrip']//input[@value='RoundTrip']"))).click();
    
  • 浏览器快照:

Spicejet_RoundTrip


参考文献

您可以在以下位置找到几个相关的详细讨论: