Selenium单击并等待

Selenium单击并等待,selenium,cucumber,Selenium,Cucumber,我有一个注册表单,可以注册很多用户,当我点击创建表单时,第一个循环中的问题太快了,第一个没有注册,第二个没有注册。。。, 所以我使用线程。sleep(500) 我想避免使用睡眠 有办法吗 这是我的密码 @Given("user on registration page and create users") public void user_on_registration_page_and_create_users() throws InterruptedExcept

我有一个注册表单,可以注册很多用户,当我点击创建表单时,第一个循环中的问题太快了,第一个没有注册,第二个没有注册。。。, 所以我使用线程。sleep(500) 我想避免使用睡眠 有办法吗

这是我的密码

 @Given("user on registration page and create users")
    public void user_on_registration_page_and_create_users() throws InterruptedException {

        System.out.println(userLoginPageDataList);

        for(UserLoginPageData userLoginPageData:userLoginPageDataList){
            userRegistrationPage.init();
            logger.info("*************************************** init the driver && go to registration page http://localhost:4200/register");

            logger.info("*************************************** reading line "+userLoginPageData.getRowIndex() +" from Excel file");
            userRegistrationPage.enterUserLogin(userLoginPageData.getUsername());
            userRegistrationPage.enterUserPassword(userLoginPageData.getPassword());
            userRegistrationPage.enterUserRole(userLoginPageData.getUserRole());
            userRegistrationPage.clickOnCreate();
         //   Thread.sleep(500);

            logger.info(userLoginPageData.getUsername()+" is registred");

        }
    }
您可以使用显式(智能)等待


阅读更多有关的信息(当您使用
PageFactory
时),一个可能的解决方案是实现您自己的
定位器
,该定位器可以从
AjaxElementLocator
扩展

假设您有一个表单,该表单具有一些值得注意的属性,表示它已准备好接受输入(这可能是某个按钮状态或显示某个标签等)

因此,如果满足该条件,您可以按照其字段“可用”的方式初始化页面对象

这可以通过在
PageFactory.init()
中使用自定义的
定位器来实现

例如,这里是两个字段的形式。表示已准备好交互的条件是
create
按钮启用:

类MyForm{
@FindBy(id=“用户”)
网络元素用户;
@FindBy(id=“创建”)
网页元素创建;
公共MyForm(SearchContext SearchContext){
PageFactory.initElements(字段->新AjaxElementLocator(searchContext,字段,10){
@凌驾
受保护的布尔值IsElementAvailable(WebElement元素){
返回create.isEnabled();
}
},这个);
}
}
除非启用了
create
按钮,否则任何调用字段方法的尝试都将失败,脚本将在10秒内重试失败


有关如何对页面对象使用条件的详细信息,请参见。

我使用的是页面工厂,我使用的是相同的wait.until(ExpectedConditions.elementToBeClickable(By.id(“create”))。单击();你有这么多不同类型的预期条件,使用最合适的。在我的回答中,我的trid只是向您介绍显式等待。是的,我的问题是在单击确定之后,所以在您单击userRegistrationPage.clickOnCreate()之后;你面对的是isse。你能详细描述一下,点击后的问题是什么,你期望的是什么,wat是实际的行为。好的,我有excel文件,我在其中循环,抵制用户,在第一次测试中,第一行没有注册,另一行注册,第二次运行测试,它注册了所有行,但当使用线程时,它会在第一次测试中注册所有行
WebDriverWait w = new WebDriverWait(driver, 5); //will wait 5 seconds most , but if element is visuble in the third second it will wait 3 sec.
w.until(ExpectedConditions.visibilityOfElementLocated(By.id("submit_btn")));