java - 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");
}
}
我该如何解决这个问题?
解决方案
我同意@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']"))
推荐阅读
- java - “builder.replace”是什么类型的复杂性?
- enterprise-architect - 在 Gitbook 中使用 Enterprise Architect API 获取文档
- python - 如何在 Django 中删除不可为空的字段?
- javascript - 将 HyperApp Web 应用程序上传到 github,我要上传哪些文件?
- c++ - constexpr(即常量初始化)模板变量的初始化顺序是否有保证?
- firefox - Imacro 脚本在 Firefox 中不起作用
- tsql - 根据分组值创建序列号
- python - 如何使用 open() 打开特定路径?
- python - 让 LED 在莫尔斯中闪烁点或破折号
- css - 将彩色不透明度应用于 SVG 图像