Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SeleniumWebDriver中等待输入长度为5个字符的文本_Selenium_Selenium Webdriver_Webdriver - Fatal编程技术网

如何在SeleniumWebDriver中等待输入长度为5个字符的文本

如何在SeleniumWebDriver中等待输入长度为5个字符的文本,selenium,selenium-webdriver,webdriver,Selenium,Selenium Webdriver,Webdriver,我正在尝试自动化一个涉及验证码的应用程序。 我尝试使用WebDriverWait,代码如下: WebDriverWait mywait = new WebDriverWait(driver,20); mywait.until(ExpectedConditions.attributeToBeNotEmpty(driver.findElementByClassName("loginCaptcha"),"value")); 现在的问题是,只要我在文本中输入captcha的第一个字符,上面的条件就变为

我正在尝试自动化一个涉及验证码的应用程序。 我尝试使用WebDriverWait,代码如下:

WebDriverWait mywait = new WebDriverWait(driver,20);
mywait.until(ExpectedConditions.attributeToBeNotEmpty(driver.findElementByClassName("loginCaptcha"),"value"));
现在的问题是,只要我在文本中输入captcha的第一个字符,上面的条件就变为真,接下来的语句将被执行,这将导致无效的captcha错误

是否有办法等待文本框中输入5个字符(我的验证码长度始终为5个固定字符。)


PS:我不想使用静态等待

您应该使用一些预期条件,如等待直到“提交”按钮可单击(如果该按钮呈灰色),直到您键入5个字符,或者将第五个验证码字符保存在变量中,然后等待直到在输入元素中键入第五个字符。

您可以使用以下方法执行此操作:
  • FluentWait

    private Wait<WebDriver> wait = new FluentWait<>(yourWebDriverInstance).withTimeout(10, TimeUnit.SECONDS).pollingEvery(500, TimeUnit.MILLISECONDS).ignoring(NoSuchElementException.class);
    
    用法:

    WebElement e = driver.findElementByClassName("loginCaptcha");
    waitUntilElemValueLengthEquals(e,5);
    
    旁注:

    WebElement e = driver.findElementByClassName("loginCaptcha");
    waitUntilElemValueLengthEquals(e,5);
    

    对于实际的实现,如果您可以创建一个实现WebDriver接口的类,那就更好了。该类将包含所有WebDriver接口方法和所有自定义方法(如上述方法)。

    首先,您不应该尝试使用selenium解决CAPTCHA问题。验证码的全部目的是防止UI自动化。如果您想克服CAPTCHA,您应该使用SUT的内部API

    关于等待特定的文本长度,应该是这样的:

    //First init WebDriverWait to 20 sec
    WebDriverWait mywait = new WebDriverWait(driver, 20);
    
    //Locator to your DOM element
    By byClass = By.class("loginCaptcha");
    
    //Wait until text box has value greater or equal 5 character
    mywait.until(new ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver d) {
                        return (d.findElement(byClass).getAttribute ("value").length() >= 5)
                    }
                });
    
    //第一次初始化WebDriverWait到20秒
    WebDriverWait mywait=新的WebDriverWait(驱动程序,20);
    //DOM元素的定位器
    By byClass=By.class(“loginCaptcha”);
    //等待文本框的值大于或等于5个字符
    mywait.until(新的ExpectedCondition(){
    公共布尔应用(WebDriver d){
    返回(d.findElement(byClass).getAttribute(“值”).length()>=5)
    }
    });
    
    //First init WebDriverWait to 20 sec
    WebDriverWait mywait = new WebDriverWait(driver, 20);
    
    //Locator to your DOM element
    By byClass = By.class("loginCaptcha");
    
    //Wait until text box has value greater or equal 5 character
    mywait.until(new ExpectedCondition<Boolean>() {
                    public Boolean apply(WebDriver d) {
                        return (d.findElement(byClass).getAttribute ("value").length() >= 5)
                    }
                });