【问题标题】:NoSuchElementException in Selenium WedDriver TestSelenium WebDriver 测试中的 NoSuchElementException
【发布时间】:2014-10-16 18:24:50
【问题描述】:

我是 selenium 新手,写了一个基本测试来启动一个网站,点击链接,填写表格并提交。

一旦到达注册页面,它就无法找到“名字”文本框。我使用萤火虫仔细检查过,它只在一个地方可用。我什至尝试用 xpath 识别元素仍然得到相同的错误。

这是使用 xPath 识别名字文本框的代码。

package Default;

import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.Select;
//import org.openqa.selenium.firefox.FirefoxDriver;

public class FirstWDWithoutRecording {

@Test
public void SouthWestSignUp() throws InterruptedException
{

    //Open the FF/Chrome browser
    //FirefoxDriver oBrw = new FirefoxDriver();
    ChromeDriver oBrw = new ChromeDriver();

    //Maximize Browser
    oBrw.manage().window().maximize();

    //Open/Launch www.southwest.com
    System.setProperty("webdriver.chrome.driver", "E://chromedriver.exe");
    oBrw.get("http://www.southwest.com/");

    //Click on Sign up and Save
    //Recognising
    oBrw.findElement(By.linkText("Sign up")).click();

    Thread.sleep(7000);

    //Enter First Name
    oBrw.findElement(By.xpath("//input[@id='FIRST_NAME']")).clear();
    oBrw.findElement(By.xpath("//input[@id='FIRST_NAME']")).sendKeys("abc");

    //Enter Last Name
    oBrw.findElement(By.id("LAST_NAME")).clear();
    oBrw.findElement(By.id("LAST_NAME")).sendKeys("Kish123");

    //Enter Email ID
    oBrw.findElement(By.id("EMAIL")).clear();
    oBrw.findElement(By.id("EMAIL")).sendKeys("abc@Kish123.com");

    //Selecting Home Airport
    Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT")));
    uiHomeAp.deselectByVisibleText("Atlanta, GA - ATL");

    //Accepting Conditions
    oBrw.findElement(By.id("IAN")).click();

    //Click Submit
    oBrw.findElement(By.id("submit")).click();

   }

}

【问题讨论】:

  • 如果您将搜索直接应用到southwest.com/html/email/… 而不是从主页转发,会发生什么情况。这行得通吗?
  • 不,它没有工作仍然得到同样的错误

标签: selenium xpath webdriver automated-tests


【解决方案1】:

您的注册表单位于<iframe> 中。为了让 Webdriver 能够“看到”表单,您首先需要切换到该 iframe。

driver.switchTo().frame(0);   //'0' as it is the only iframe on the page, the value is the index of all iframes on the page
//do your login actions
//after return
driver.switchTo().defaultContent();

【讨论】:

  • 感谢它现在有效。但是 selenium 无法从下拉列表中选择值。 //选择家乡机场 Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT"))); uiHomeAp.deselectByVisibleText("Atlanta, GA - ATL");
  • @KishoreJawalkar 选择元素仍在 'iframe. Don't exit that iframe, using the .switchTo().defaultContent();` 中,直到您完成与 iframe 中元素的所有交互。
  • @MarkRowlands 只有我一个人想知道你是如何猜到 iframe 的吗? :)
  • @olyv 我曾经不得不在一个框架内框架内嵌套框架的网站上工作。噩梦!
  • @MarkRowlands 我的选择语句在 iframe 中。但它仍然无法正常工作。 //选择家乡机场 Select uiHomeAp = new Select(oBrw.findElement(By.id("HOME_AIRPORT"))); uiHomeAp.deselectByVisibleText("亚特兰大,乔治亚州 - ATL"); //返回后 oBrw.switchTo().defaultContent();
猜你喜欢
  • 2017-10-15
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 2019-08-25
  • 2012-04-24
  • 2014-05-11
  • 1970-01-01
  • 2012-12-21
相关资源
最近更新 更多