使用Java的Selenium:无法识别弹出窗口中的元素

时间:2019-05-24 11:18:24

标签: java selenium

在www.jobserve.com中,当您单击“应用”(应用于任何职位发布)时,将弹出“求职申请”。

如何告诉Selenium从此弹出窗口中读取元素?目前,硒元素无法识别弹出窗口中的所有元素。

让我们说我要上传我的简历,那我该怎么办?

我尝试过切换到框架,但弹出窗口似乎不是框架,因此无法正常工作。

3 个答案:

答案 0 :(得分:1)

最后我做到了。使用Selenium Recorder插件,我发现我需要切换到第0帧(请参见所附的屏幕截图)。答案是:

driver.switchTo().frame(0);

Screenshot

答案 1 :(得分:0)

您可以使用以下任何一种方法来处理弹出窗口。

1)无需切换到模式/框架。您可以直接使用findById,名称或类移动到该模式元素。

2)如果您要上传任何文件,请遵循以下几点:

a)将文件上传到服务器上tmp目录中以进行临时备份。像

String path = FILE_UPLOAD_COMMON_PATH + File.separatorChar + file.getName();

try(FileOutputStream fileOutputStream = new FileOutputStream(path)){
    fileOutputStream.write(bs); // byte[] bs
}catch(Exception e) {
    throw e;
}

b)现在从tmp目录获取并使用类似驱动程序上传文件,

String path = FILE_UPLOAD_COMMON_PATH + File.separatorChar + file.getName();
driver.findElements(By.id("files")).get(0).sendKeys(path);

答案 2 :(得分:0)

要在 iFrames 之间切换,我们必须使用驱动程序的switchTo().frame命令。我们可以通过三种方式使用switchTo().frame()

switchTo.frame(int frameNumber)::传递帧索引,驱动程序将切换到该帧。

switchTo.frame(string frameNameOrId):传递框架元素名称或ID,驱动程序将切换到该框架。

switchTo.frame(WebElement frameElement)::传递框架Web元素,驱动程序将切换到该框架。

    WebDriver driver = new FirefoxDriver();
    driver.get("https://www.jobserve.com");

    WebDriverWait wait = new WebDriverWait(driver, 10);
    WebElement element = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//a[contains(text(),'Job Search')]")));
    element.click();    

    driver.findElement(By.xpath("/html/body/form/div[8]/div[1]/a/span")).click();

    driver.findElement(By.xpath("//*[@id=\"searchtogglelink\"]")).click();

    WebDriverWait wait1 = new WebDriverWait(driver, 10);
    WebElement element1 = wait1.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"td_apply_btn\"]")));
    element1.click();
    Thread.sleep(2000);
    JavascriptExecutor exe = (JavascriptExecutor) driver;
    Integer numberOfFrames = Integer.parseInt(exe.executeScript("return window.length").toString());
    System.out.println("Number of iframes on the page are " + numberOfFrames);

    driver.switchTo().frame(0);

    WebElement Upload_File = driver.findElement(By.xpath("//input[@id='filCV']"));
    Upload_File.sendKeys("Path of file");

使用switchTo().frame()可以切换到该框架并对该打开的框架执行所有操作。

有两种方法可以找到网页中iFrame的总数。首先是执行JavaScript,其次是找到标记名称为iFrame的Web元素总数。这是使用这两种方法的代码:

//By executing a java script
JavascriptExecutor exe = (JavascriptExecutor) driver;
Integer numberOfFrames = Integer.parseInt(exe.executeScript("return 
window.length").toString());
System.out.println("Number of iframes on the page are " + numberOfFrames);

//By finding all the web elements using iframe tag
List<WebElement> iframeElements = driver.findElements(By.tagName("iframe"));
System.out.println("The total number of iframes are " + 
iframeElements.size());

i)按索引切换到帧- iFrame的索引是它在HTML页面中的位置。在此页面上仅存在一帧。要切换到第0个iframe,我们可以简单地编写 driver.switchTo()。frame(0)

//Switch by Index
 driver.switchTo().frame(0);

ii)切换到按名称显示的框架- 名称属性的值是iframe1。我们可以使用命令 driver.switchTo()。frame(“ iframe1”)来使用该名称切换到iFrame。

 //Switch by frame name
 driver.switchTo().frame("iframe1");

iii)切换到ID框架-  iFrame广告代码中我们还具有ID属性。我们也可以使用它来切换到框架。我们要做的就是将id传递给switchTo命令,例如 driver.SwitchTo()。frame(“ IF1”)。

//Switch by frame ID 
driver.switchTo().frame("IF1");

iv)通过WebElement切换到框架-

现在,我们只需将iFrame WebElement传递给 driver.switchTo()。frame() 命令即可切换到iFrame。首先使用任何定位器策略找到iFrame元素,然后将其传递给switchTo命令。

//First find the element using any of locator stratedgy
WebElement iframeElement = driver.findElement(By.id("IF1"));

//now use the switch command
driver.switchTo().frame(iframeElement);

有关更多信息,请通过this链接。