2012-09-05 2 views
3

Selenium WebDriver (2.21.0)를 사용하여 웹 요소를 클릭하려고합니다.Selenium 클릭 동작은 Selenium IDE가 아닌 다른 페이지로 연결

Selenium IDE를 통해 운전 해보면 정상적으로 작동하지만 Java 구현 인 Firefox 드라이버를 사용하여 동일한 동작을 시도하면 잘못된 페이지가 표시됩니다.

코드가 실행되는 동안 수동으로 원하는 요소로 스크롤하면 작동합니다.

나는 웹 요소가 표시되는 것을 확인하고 일부 요소가 아니라 내가 기대하고있는 하나를 반환

By by = By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]"); //**Edit:** this is how i 
                     //am getting the locator 

WebElement element = driver.findElement(by); 


return (element.isEnabled() || element.isDisplayed()) ? element : null; 

를 사용하여 활성화.

Selenium webdriver는 주로 화면에 표시되지 않는 경우 요소 자체로 스크롤하고 필요한 상호 작용을 수행하므로 이상하게 보입니다.

one, two과 같은 답변을 시도했지만 성공하지 못했습니다.

미리 감사드립니다. 편집

: 여기는 IDE의 수출 코드 (자바/JUnit4/webdriver)입니다

package com.example.tests; 

import java.util.regex.Pattern; 
import java.util.concurrent.TimeUnit; 
import org.junit.*; 
import static org.junit.Assert.*; 
import static org.hamcrest.CoreMatchers.*; 
import org.openqa.selenium.*; 
import org.openqa.selenium.firefox.FirefoxDriver; 
import org.openqa.selenium.support.ui.Select; 

public class Bandar { 
    private WebDriver driver; 
    private String baseUrl; 
    private StringBuffer verificationErrors = new StringBuffer(); 
    @Before 
    public void setUp() throws Exception { 
     driver = new FirefoxDriver(); 
     baseUrl = "http://e.weibo.com/"; 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
    } 

    @Test 
    public void testBandar() throws Exception { 
     driver.get(baseUrl + "/nescafechina"); 
     driver.findElement(By.xpath("(//a[contains(@href, 'javascript:void(0);')])[26]")).click(); 
     driver.findElement(By.xpath("(//a[contains(@href, 'javascript:void(0);')])[12]")).click(); 
    } 

    @After 
    public void tearDown() throws Exception { 
     driver.quit(); 
     String verificationErrorString = verificationErrors.toString(); 
     if (!"".equals(verificationErrorString)) { 
      fail(verificationErrorString); 
     } 
    } 

    private boolean isElementPresent(By by) { 
     try { 
      driver.findElement(by); 
      return true; 
     } catch (NoSuchElementException e) { 
      return false; 
     } 
    } 
} 
+0

Selenium IDE의 기능을 여기에 붙여 넣을 수 있습니까? –

+0

응답 해 주셔서 감사합니다 Mr.Curtis! Selenium IDE의 내 보낸 테스트 케이스를 붙여 넣었다. –

+0

Selenium IDE에서 만든 것보다 더 좋은 위치 지정 코드를 제공해야한다고 생각합니다. 나는 성공없이 FirePath와 Firebug에서 생성 된 XPath 로케이터를 시도했다. –

답변

1

Ishank, 내가 통해 사라 수행하고 다른 종류를 표시하는 테스트를 만들었습니다 무엇

테스트에서 사용할 수있는 주장의 그들은 당신이보고있는 것과 조금 다르게 보입니다, 당신이 주된 문제는 실제 요소를주지 않기 때문에 WebElement element = driver.findElement(by);이라고 느낍니다. (by); 섹션은 페이지에서 찾을 수있는 문자열을 찾고 있습니다. 사용할 수있는 문자열은 다음과 같습니다. id ("gbfqb"); 또는 xpath ("(//a[contains(@href, 'javascript:void(0);')])[26]"); 또는 심지어 ("find-button");입니다.

/** 
* Test the main Google page. 
* @throws InterruptedException 
* 
*/ 
@Test 
public void signUp() throws InterruptedException { 

String testId = "TestStack01"; 

entered(testId); 

webDriver.get("www.google.com"); 
webDriver.findElement(By.id("gbqfq")).clear(); 
webDriver.findElement(By.id("gbqfq")).sendKeys("Test"); 
assertEquals("", webDriver.findElement(By.id("gbqfb")).getText()); 

WebElement whatyourlookingfor = webDriver.findElement(By.id("gbqfb")); 
assertTrue(selenium.isElementPresent("gbqfb")); 
assertTrue(whatyourlookingfor.isEnabled()); 
assertTrue(whatyourlookingfor.isDisplayed()); 
assertFalse(whatyourlookingfor.isSelected()); 

webDriver.findElement(By.id("gbqfb")).click(); 

leaving(testId); 

} 

반환되는 요소를 가져 오는 데 도움이되기를 바랍니다.

커티스 밀러

+1

안녕 커티스, ur 시간 동안 고마워! 코드 스 니펫을 편집했습니다. By-By.xpath ("(// href, 'javascript : void (0);')]) [26] ] "); 그래서, 올바른 로케이터가 driver.findElement (by)에 들어가기를 바랍니다. .. –

+0

Xpath의 자바 스크립트가 문제를 일으킬 수있는 것처럼 보입니다. 'selenium.fireEvent ("name = title", "blur")와 같은 것을 시도해 볼 수도 있습니다. '자바 스크립트 액션이 수행 된 후에. –

관련 문제