2017-12-15 26 views
0

Selenium WebDriver를 사용하여 다양한 텍스트 상자에 특정 값을 입력하고 Try-Block 및 If-Else 문을 사용하여 특정 웹 기반 응용 프로그램 그런 다음 해당 개별 요소를 기반으로 적절한 값을 제공하십시오. 내 문제는 if-else 문을 읽을 때마다 else {block에 "error"를 출력하기 때문에 if 문이 "false"로 읽혀지는 것 같습니다.Selenium WebDriver에서 if-else 문을 사용하여 웹 요소에 값 찾기 및 제공

나는 이미 element.getAttribute ("id")의 String 값을 검사했으며, if-else 문에서 지정한 값과 같습니다.

일부 파고를했는데 요소 가시성 및 element.isEnabled() 사용과 관련된 if-else 문을 발견 한 동안 element.getAttribute() 문자열 값을 사용하여 특정 항목을 찾지 못했습니다. 웹 요소를 만들고 값을 보냅니다. 여기

내 코드입니다 :

WebDriver 드라이버;

@Test 
public void enterInfo() throws Exception { 

    System.setProperty("webdriver.gecko.driver","C:\\Users\\Kohout\\Documents\\geckoDriver\\geckodriver.exe"); 
    DesiredCapabilities capabilities = DesiredCapabilities.firefox(); 
     capabilities.setCapability("marionette", true); 
     driver = new FirefoxDriver(); 

     driver.get("http://www.firstcry.com/"); 
     Thread.sleep(3000L); 

     WebElement element = driver.findElement(By.id("Email")); 
     typingInfo(element); 

     Thread.sleep(2000L); 

     element = driver.findElement(By.id("mobileprefix")); 
     typingInfo(element); 

     Thread.sleep(2000L); 


     element = driver.findElement(By.id("Mobile")); 
     typingInfo(element); 
    } 



public void typingInfo(WebElement element) throws AWTException { 

    String email = "[email protected]"; 
    String countryCode = "1"; 
    String phoneNumber = "555-318-9357"; 

    try { 

     if(element.getAttribute("id") == "Email") { 

      System.out.println("Commencing command..."); 

      element.sendKeys(email); 

     } else if (element.getAttribute("id") == "mobileprefix") { 

      System.out.println("Commencing command..."); 

      element.sendKeys(countryCode); 
     } else if (element.getAttribute("id") == "Mobile") { 

      System.out.println("Commencing command..."); 

      element.sendKeys(phoneNumber); 

     } else { 

      System.out.println("Error"); 
     } 

    } catch (Throwable t) { 

     System.out.println(t.getStackTrace()); 

    } 

귀하의 도움과 제안에 감사드립니다.

+0

당신이'else' 블록의 속성을 인쇄 할 수 던져? 'System.out.println (element.getAttribute ("id")); System.out.println ("Error"); ' – Buaban

+0

문자열을 비교할 때 == 대신 equals() 함수를 사용해야합니다. 아래 문제에 대한 중대한 답변. –

답변

0

이 시도 :

if("Email".equals(element.getAttribute("id"))) { 
     System.out.println("Commencing command..."); 
     element.sendKeys(email); 
    } else if ("mobileprefix".equals(element.getAttribute("id"))) { 
     System.out.println("Commencing command..."); 
     element.sendKeys(countryCode); 
    } else if ("Mobile".equals(element.getAttribute("id"))) { 
     System.out.println("Commencing command..."); 
     element.sendKeys(phoneNumber); 
    } else { 
     System.out.println("Error"); 
    } 
  • 항상 자바에서 문자열 비교에 대한 equals를 사용합니다. ==은 개체를 검사합니다.
  • NullPointerException을 피하기 위해 문자열 리터럴에서 equals으로 전화하십시오.
+0

그게 다야! 문자열 값을 비교할 때 equals() 함수를 사용해야한다는 사실을 잊어 버렸습니다. 도와 주셔서 감사합니다! –

+0

@JordanKohout 잘 듣고 싶습니다. _thank you_를 말하는 stackoverflow- 방법은 대답과 upvote를 수락하는 것입니다. 그렇게하면 명성을 얻게됩니다. –

+0

물론 공유해 주셔서 감사합니다. 너는 상경을 받았다. 선생님/부인. –

0

== 연산자를 사용하면 문자열 비교가 제대로 작동하지 않기 때문에 생각합니다. 코드가 수정되었습니다.

공공 무효 typingInfo (WebElement 요소)를 사용해 AWTException가 {

String email = "[email protected]"; 
String countryCode = "1"; 
String phoneNumber = "555-318-9357"; 

try { 

    if("Email".equalsIgnoreCase(element.getAttribute("id")) { 

     System.out.println("Commencing command..."); 

     element.sendKeys(email); 

    } else if ("mobileprefix".equalsIgnoreCase(element.getAttribute("id")) { 

     System.out.println("Commencing command..."); 

     element.sendKeys(countryCode); 
    } else if ("Mobile"..equalsIgnoreCase(element.getAttribute("id")) { 

     System.out.println("Commencing command..."); 

     element.sendKeys(phoneNumber); 

    } else { 

     System.out.println("Error"); 
    } 

} catch (Throwable t) { 

    System.out.println(t.getStackTrace()); 

} 
+0

우수! "IgnoreCase"를 추가해 주셔서 감사합니다. 매우 유용합니다. –