2015-10-20 72 views
0

我經常收到 「沒有這樣的元素異常」 爲 「名」 測試箱硒WD - southwest.com

下面是我的代碼:

public class southwestSignUpSave { 
WebDriver oBrw; 

@Before 
public void loadwebsite(){ 
    oBrw = new FirefoxDriver(); 
    oBrw.manage().window().maximize(); 
    oBrw.get("https://southwest.com"); 
    oBrw.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 

} 

@Test 
public void signUpAndSave(){ 

    oBrw.findElement(By.partialLinkText("OFFERS")).click(); 
    oBrw.findElement(By.partialLinkText("Sign")).click(); 
    //oBrw.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    WebDriverWait oWait = new WebDriverWait(oBrw, 30); 
    oWait.until(ExpectedConditions.presenceOfElementLocated(By.id("FIRST_NAME"))); 

    oBrw.findElement(By.id("FIRST_NAME")).clear(); 
    oBrw.findElement(By.id("FIRST_NAME")).sendKeys("abc"); 
    oBrw.findElement(By.id("LAST_NAME")).clear(); 
    oBrw.findElement(By.id("LAST_NAME")).sendKeys("asd"); 
    oBrw.findElement(By.id("EMAIL")).clear(); 
    oBrw.findElement(By.id("EMAIL")).sendKeys("[email protected]"); 
    new Select(oBrw.findElement(By.id("HOME_AIRPORT"))).selectByVisibleText("Akron/Canton, OH - CAK"); 
    oBrw.findElement(By.id("IAN")).click(); 


} 

}

我試着使用ID和名稱。

我在哪裏出錯了。我是Selenium的新手WD

回答

0

U可以嘗試使用xpath查找元素。爲此,您需要在firefox中安裝firepath插件,然後使用firepath檢查元素。

oBrw.findElement(By.xpath("copy paste the xpath here")).clear(); 

我還建議使用System屬性內的loadwebsite()方法加載驅動程序。

System.setProperty("webdriver.firefox.driver", "//your driver path"); 
oBrw=new FirefoxDriver(); 
如果登錄頁面在新標籤/窗口打開

然後u需要,因爲硒在開幕選項卡的默認停留導航到標籤/窗口。導航u需要點擊「註冊」之後,以下面的代碼行添加 -

Set<String> s=wd.getWindowHandles(); 
Iterator<String> it=s.iterator(); 
it.next();//control goes to 1st default tab 
String str=it.next().toString();//control goes to the next tab 
oBrw.switchTo().window(str);//driver switches to the new window/tab. 

如果該元素存在於框內然後也u需要第一內側它找到元件之前切換到該幀。以下是代碼 -

WebElement web=oBrw.findElement(By.xpath("copy paste your frame xpath")); 
oBrw.switchTo.frame(web); 

現在嘗試查找新選項卡/窗口中存在的元素。

0

該文本框位於iFrame內,因此您需要先切換到該iFrame,然後嘗試使用findElement方法來查找文本框。

oBrw.findElement(By.partialLinkText("OFFERS")).click(); 
      oBrw.findElement(By.partialLinkText("Sign")).click(); 

      oBrw.switchTo().defaultContent(); 
      oBrw.switchTo().frame(0);   
      WebElement id = oBrw.findElement(By.name("FIRST_NAME")); 
      id.sendKeys("USERNAME"); 

希望這會有所幫助。

+0

是這個作品..只是多了一個問題,oBrw.switchTo()。幀(0) – user5465511

+0

你看什麼參數幀(0)中的「0」 – user5465511

0

FIRST NAME輸入文本字段在iframe之內。檢查下面的一段HTML。

<iframe frameborder="0" src="https://luv.southwest.com/servlet/formlink/f?kOHpjQACAY" onload="scroll(0,0);" verticalscrolling="no" horizontalscrolling="no" scrolling="no" title="Click 'n Save signup form"></iframe> 
 
<html dir="ltr"> 
 

 
<head> 
 

 
    <body> 
 
    <p> 
 
     <span class="required">*Required</span> 
 
    </p> 
 
    <div class="clear"></div> 
 
    <form id="cnsForm" onsubmit="return validateForm();" action="https://luv.southwest.com/servlet/campaignrespondent" method="POST"> 
 
     <div class="form_field first_name"> 
 
     <label for="first_name"> 
 
      <input id="FIRST_NAME" type="text"=""="" maxlength="25" size="22" name="FIRST_NAME"> 
 
     </div> 
 
...

因此硒是無法找到的元素。這裏我們需要顯式地切換到iframe,如下所示。在找到FIRST_NAME之前插入以下代碼段。 (您可以插入iframe的良好的格式化的XPath,我只是抓住它從螢火蟲)

WebElement iframeSwitch = oBrw.findElement(By.xpath("/html/body/div[1]/div[3]/div[2]/div[1]/div/div/div[4]/div/div/div/div[3]/iframe")); 
      oBrw.switchTo().frame(iframeSwitch);