Selenium WD无法在cheaptickets.in的弹出窗口中找到webelement

时间:2018-09-02 18:47:10

标签: java selenium selenium-webdriver xpath

在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");
    }
}

如何解决此问题?

2 个答案:

答案 0 :(得分:0)

@SuperShazam看起来像此定位器在DOM中找到13个元素,这可能是Selenium引发异常的原因。尝试为每个元素使用更具体的定位器。如果您无法创建更多特定的定位器,请尝试遍历元素并检查所需的元素是否可见。

答案 1 :(得分:0)

我同意@baadnews,您在代码中用于获取Email输入字段的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']"))