java - Selenium WebDriver Java - 首次打开页面时出现的注册表单
问题描述
我是第一次打开一个页面,但是在成功加载页面后,一个注册表单马上就来了。我想在打开的页面上工作,但如果不处理页面加载时立即出现的注册表单,这是不可能的,但问题是我无法将控件切换到正在打开的注册表单。我使用 Selenium WebDriver 和 Java 作为脚本语言。
由于这个问题与我的一些官方项目有关,所以我无法分享确切的 URL,但我设法找到了另一个与我面临问题的 URL 类似的 URL。我在下面提供了替代 URL:
http://way2automation.com/way2auto_jquery/index.php
在上面的 URL 中,我想在加载的页面中工作,但在处理注册表单之前无法完成。请告诉我如何使用 Selenium WebDriver 和 Java 作为语言将控制切换到注册表单。
解决方案
您必须使用链式搜索概念查找元素。上述注册弹出窗口在 iframe中不可用。因此,首先您必须找到注册表单的父 WebElement,然后您可以使用父元素引用访问所需的元素。
//Finding the Parent Element of the Registration Form
WebElement popUp=driver.findElement(By.className("fancybox-skin"));
//Finding the Name Input Text Field using the Parent Element
popUp.findElement(By.xpath(".//*[@id='load_form']/fieldset[1]/input")).sendKeys("Subburaj");
同样,您可以使用弹出元素(父元素)访问所有适用的注册表单元素
注意:我已经测试了上面的代码,它工作正常
编辑:完整代码:
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.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Way2Automation {
public static void main(String args[]){
System.setProperty("webdriver.chrome.driver", "drivers/chromedriver.exe");
WebDriver driver=new ChromeDriver();
driver.get("http://way2automation.com/way2auto_jquery/index.php");
//Explicit wait is added after the Page load
WebDriverWait wait=new WebDriverWait(driver,20);
wait.until(ExpectedConditions.titleContains("Welcome"));
WebElement popUp=driver.findElement(By.className("fancybox-skin"));
popUp.findElement(By.xpath(".//*[@id='load_form']/fieldset[1]/input")).sendKeys("Subburaj");
popUp.findElement(By.xpath(".//*[@id='load_form']/fieldset[2]/input")).sendKeys("987654321");
}
}
推荐阅读
- mysql - MySQL join 是扫描表而不是使用索引
- javascript - 循环数组并根据数组中的项目值创建“n”个其他数组
- java - 使用 ftl 模板和 Spring Boot 进行动态页面更新
- java - 在 popOS 上安装 icedtea-8-plugin
- python - 需要一些简单的python在字符串中查找的帮助
- java - 从 MediaCodec 将相同的视频渲染到 2 个表面
- python - 在python中并行运行两个函数并将一个函数返回的参数作为参数传递给另一个函数
- visual-studio-code - 如何在 VsCode 终端中运行 MSyS2 Mingw64 bash 以及 cmd 提示符?
- powerbi - 如何在 PowerBI 中创建包含计算单元格而不是列的表格?
- c - 土耳其元音字母识别