[英]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.