Java 使用Selenium单击IRCTC上不可见的按钮
Irctc上的Java 使用Selenium单击IRCTC上不可见的按钮,java,selenium-webdriver,Java,Selenium Webdriver,Irctc上的立即预订按钮不可单击。 显示的错误是不可交互的元素。 我试着用 wait.until(ExpectedConditions.elementToBeClickable(By.xpath(""))); 但仍然没有收获 FirefoxOptions options = new FirefoxOptions(); options.setBinary("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe"); //This is th
立即预订
按钮不可单击。
显示的错误是不可交互的元素。
我试着用
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("")));
但仍然没有收获
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.
注意:我不鼓励IRCTC的自动化。这只是为了引导OP朝着正确的方向前进。使用风险自负。 我正在使用
ChromeDriver
,您可以使用任何驱动程序,但请确保它支持Javascript
。这是密码
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()");
}
}
}
您的代码有几个问题。首先,ID为cllink-13237-CC-1的元素不存在。确保获得元素的确切ID。由于IRCTC不允许右键单击,因此可以使用Ctrl+Shift+I来查看源代码
其次,
BookNow
元素被隐藏,因此我使用Javascript
点击它。页面上不存在id为cllink-13237-CC-1
的元素。你打算选哪趟车?我在代码中提到的车次号是错误的。否则,该行代码将在Firefox浏览器中成功执行。感谢您的解决方案,它现在工作正常。您能建议我应该在哪里进行等待以及在哪里避免等待吗。@Sushankumar您可以删除所有线程。sleep
。只需将其保存在验证码和JavaScript执行器之前。你可能需要再做一次实验,因为我对IRCTC不太熟悉。