2

我使用 Selenium Webdriver + 修昔底德。当我尝试使用复选框(任何状态:isEnabled()、isDisplayed()、isSelected())时,会出现错误。我尝试了不同的定位器:通过 id、name、xpath。该复选框在页面源中可用。页面上的所有其他元素都可以正常工作。我使用 DisplayedElementLocatorFactory。

我的定位器:

    @FindBy(id = "remember")
//  @FindBy(xpath = ".//*[@type='checkbox']")
//  @FindBy(name = "_remember_me")
    protected WebElement rememberMeCheckbox;

复选框的 HTML 源代码:

<label for="remember" class="remember"><div class="checker" id="uniform-remember"><span><input type="checkbox" value="1" name="_remember_me" id="remember" /></span></div>Remember me</label>

我的功能:

public void isLoginFormLoadedCorrectly()
{
        String pageSource = driver.getPageSource();
        System.out.println(pageSource);

        String errorMessage = "";

        if (!loginInput.isDisplayed())
            errorMessage += "Username field is not displayed or not found\r\n";
        if (!passwordInput.isDisplayed())
            errorMessage += "Password field is not displayed or not found\r\n";
        if (!submitButton.isDisplayed())
            errorMessage += "Submit button is not displayed or not found\r\n";
        if (!passwordRecoveryLink.isDisplayed())
            errorMessage += "Password recovery link is not displayed or not found\r\n";
        if (!rememberMeCheckbox.isDisplayed())
            errorMessage += "Remember me check-box is not displayed or not found\r\n";
    //    if (rememberMeCheckbox.isSelected())
    //        errorMessage += "Remember me check-box is selected\r\n";

        assertThat(errorMessage, errorMessage.equals(""), is(true));
    }

错误:net.thucydides.core.webdriver.WebdriverAssertionError:org.openqa.selenium.NoSuchElementException:30 秒后超时。无法找到元素原因:org.openqa.selenium.NoSuchElementException:30 秒后超时。无法找到元素原因:org.openqa.selenium.NoSuchElementException:元素不可用

4

2 回答 2

1

我在尝试单击复选框时遇到了同样的错误。我通过单击带有标签的元素来解决它<span>,而不是<input>

例如,在您的示例中,可以像这样找到它(元素比您的复选框高一级):

@FindBy(xpath = ".//*[@type='checkbox']/..")
于 2013-10-11T09:23:12.253 回答
0

我无法重现您的问题:

@RunWith(ThucydidesRunner.class)
public class VerificationTest {

    @Managed
    public WebDriver driver;

    @ManagedPages
    public Pages pages;

    @Test
    public void testCheckbox(){
        CheckBoxPage pg = pages.get(CheckBoxPage.class);
        pg.openAt("http://www.echoecho.com/htmlforms09.htm");

        assertTrue("checkbox is displayed", pg.ckbxElement.isDisplayed());
        assertTrue("checkbox is selected", pg.ckbxElement.isSelected());    
    }
}

在哪里

public class CheckBoxPage extends PageObject{

    @FindBy(css = ".table8 input:checked")
    public WebElement ckbxElement;

    public CheckBoxPage(WebDriver driver) {
        super(driver);
    }

}

FindBys:org.openqa.selenium.support.FindBynet.thucydides.core.annotations.findby.FindBy

由于所有其他元素都有效,请确保:

  1. 它不在iframe
  2. html是正确的
于 2013-06-14T14:37:31.793 回答