简体   繁体   English

使用硒,单击在IRCTC上不可见的按钮

[英]Click Button which is not visible on IRCTC, using Selenium

The book now button on Irctc is not clickable. Irctc上的“立即book now按钮不可单击。 The error shown is element not interactable. 显示的错误是元素不可交互。 I tried using 我尝试使用

wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));

But still no gain. 但是仍然没有收获。

FirefoxOptions options = new FirefoxOptions();
options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //This is the location where you have installed Firefox on your machine

WebDriver driver = new FirefoxDriver(options);

WebDriverWait wait = new WebDriverWait(driver, 10);

driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);

driver.get("http://www.irctc.co.in");//It will open the website

driver.manage().window().maximize();//It will maximize the window

Thread.sleep(5000);//For Entering the Captcha before 5sec

driver.findElement(By.xpath("//input[@id='usernameId']")).sendKeys("");//enter username

driver.findElement(By.xpath("//input[@class='loginPassword']")).sendKeys("");//enter password

driver.findElement(By.xpath("//input[@id='loginbutton']")).click();//clicks on sign in

Thread.sleep(2000);

driver.findElement(By.xpath("//input[@id='jpform:fromStation']")).sendKeys("H NIZAMUDDIN - NZM");//origin station


driver.findElement(By.xpath("//input[@id='jpform:toStation']")).sendKeys("KOTA JN - KOTA");//destination station


driver.findElement(By.xpath("//input[@id='jpform:journeyDateInputDate']")).sendKeys("18-05-2017");//Date Of Journey

Thread.sleep(2000);

driver.findElement(By.xpath("//input[@id='jpform:jpsubmit']")).click();//Clicks to find the trains

Thread.sleep(2000);

driver.findElement(By.xpath("//a[@id='cllink-13237-CC-1']")).click();//Clicks the class of train to find available seats

Thread.sleep(5000);

driver.findElement(By.xpath("//a[@id='13237-3A-GN-0']")).click();//For clicking on Book Now, but is not functioning.

Note: I am not encouraging the automation of IRCTC. 注意: 我不鼓励IRCTC的自动化。 This is just to guide the OP in the right direction. 这只是为了引导OP朝正确的方向发展。 Use at your own risk. 使用风险自负。

I am using ChromeDriver , you can use any driver, but make sure it supports Javascript . 我正在使用ChromeDriver ,可以使用任何驱动程序,但请确保它支持Javascript Here is the code 这是代码

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.DesiredCapabilities;

class Main{
    public static void main(String args[])throws InterruptedException{
        System.setProperty("webdriver.chrome.driver", "C:\\Users\\Shash\\Desktop\\chromedriver.exe");
        ChromeOptions options = new ChromeOptions();
        options.addArguments("window-size=1024,768");

        DesiredCapabilities capabilities = DesiredCapabilities.chrome();
        capabilities.setCapability(ChromeOptions.CAPABILITY, options);
        WebDriver driver = new ChromeDriver(capabilities);

        driver.get("http://www.irctc.co.in");//It will open the website
        Thread.sleep(5000);//For Entering the Captcha before 5sec

        driver.findElement(By.xpath("//input[@id='usernameId']")).sendKeys("");//enter username

        driver.findElement(By.xpath("//input[@class='loginPassword']")).sendKeys("");//enter password

        driver.findElement(By.xpath("//input[@id='loginbutton']")).click();//clicks on sign in

        Thread.sleep(2000);

        driver.findElement(By.xpath("//input[@id='jpform:fromStation']")).sendKeys("H NIZAMUDDIN - NZM");//origin station


        driver.findElement(By.xpath("//input[@id='jpform:toStation']")).sendKeys("KOTA JN - KOTA");//destination station


        driver.findElement(By.xpath("//input[@id='jpform:journeyDateInputDate']")).sendKeys("18-05-2017");//Date Of Journey

        driver.findElement(By.xpath("//input[@id='jpform:jpsubmit']")).click();//Clicks to find the trains
        driver.findElement(By.xpath("//a[@id='cllink-12060-CC-0']")).click();//Clicks the class of train to find available seats
        Thread.sleep(3000);
        if (driver instanceof JavascriptExecutor) {
            ((JavascriptExecutor) driver)
                .executeScript("document.getElementById(\"12060-CC-GN-0\").click()");
        }
    }
}

Your code had several problems. 您的代码有几个问题。 First, an element with the ID cllink-13237-CC-1 did not exist. 首先,ID为cllink-13237-CC-1的元素不存在。 Make sure you get the exact ID of the element. 确保获得该元素的确切ID。 As IRCTC doesn't allow right-clicking, you can use Ctrl + Shift + I to view the source. 由于IRCTC不允许右键单击,因此可以使用Ctrl + Shift + I查看源。

Secondly, the Book Now element was hidden, therefore I used Javascript to click on it. 其次, Book Now元素被隐藏,因此我使用Javascript进行了单击。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在使用硒webdriver预订火车时如何在IRCTC中自动预订按钮? - How to automate book button in IRCTC while booking a train in selenium webdriver? 如何强制Selenium WebDriver点击当前不可见的元素? - How to force Selenium WebDriver to click on element which is not currently visible? 在Java中使用Selenium单击按钮 - Click on button using Selenium in Java 如果禁用了右键单击并且有任何方法使用webdriver处理验证码,如何在网站(如IRCTC)中获取xpath - How to get xpath in a site(like IRCTC) if right click is disabled and is there any way to handle captcha using webdriver 无法使用 Selenium 单击按钮 - Unable to Click Button Using Selenium 在使用Selenium和Java将鼠标悬停在ebay.com中的元素上之后,如何单击可见的元素 - How to click on an element which is visible after mouse hover over an element within ebay.com using Selenium and Java 我们可以使用Java和Selenium WebDriver单击与某些图像/按钮重叠的按钮吗? - Can we click a button which is overlapped by some image/button using java and selenium webdriver? 验证按钮是否可见 Selenium - Verifying that a button is visible or not Selenium 无法单击位于由弹出窗口显示的按钮 <div> 使用Selenium WebDriver - Unable to click on button which is situate on popup made by <div> using Selenium WebDriver 无法使用Selenium单击单选按钮 - Cannot click a radio button using Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM