首页 > 解决方案 > Selenium WD 无法在cheaptickets.in 的弹出窗口中找到 webelement

问题描述

在 URL"https://cheapticket.in/b2c/flights"上,单击“注册”按钮后会出现一个弹出框,我想在其中输入电子邮件和所有其他字段,但会引发以下异常:

Exception in thread "main" org.openqa.selenium.ElementNotInteractableException: 
Element <input id="" class="fluid" name="login" type="text"> could not be
scrolled into view
package TestPackage;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.GeckoDriverService;

public class CheapTickets {

    public static void main(String[] args){

        System.setProperty("driver.chrome.driver", "D:\\Selenium\\chromedriver.exe");
        WebDriver driver=new ChromeDriver();

        driver.get("https://cheapticket.in/b2c/flights");
        System.out.println("Loaded cheaptickets");

        //go to sign up
        driver.findElement(By.xpath("//a[@id='signup']")).click();
        System.out.println("Travelled to signup");
        driver.findElement(By.xpath("//input[@class='fluid']")).sendKeys("abc@abc.com");
    }
}

我该如何解决这个问题?

标签: javaseleniumselenium-webdriverxpath

解决方案


我同意@baadnews,您在代码中用于获取电子邮件输入字段的 xpath 将匹配 DOM 中的 13 个元素。因此,您的代码将获得 DOM 中的第一个匹配项,该匹配项在页面上不可见,并且您会遇到异常。

您可以像这样使用更具体的 xpath

email = driver.findElement(By.xpath("//div[@class='content']//input[@name='email']"))
mobile = driver.findElement(By.xpath("//div[@class='content']//input[@name='mobile']"))
name = driver.findElement(By.xpath("//div[@class='content']//input[@name='name']"))

推荐阅读