我想要执行-While循环,包括If Else语句

时间:2018-11-21 13:32:14

标签: java selenium for-loop conditional-statements do-while

您需要对此的回应...

我有用于的代码 1.打开Firefox 2.输入用户名和密码

检查这些参考图像

Goto https://mettl.com/corporate/live-feed#/proctoringDashboard

Click -> Select All

Click -> Authorize

Find "No more candidates awaiting Authorization"

Click -> All Tests

一旦找到候选人姓名或电子邮件地址,而不是“没有其他候选人等待授权”,请等待10秒钟,然后等待鼠标单击

像下面一样

Find Candidate Details

对于e-g

我们在线有100位候选人,当更正了候选人的详细信息后,我将复制姓名,电子邮件ID和出生日期,如果没有更正,则返回所有测试

请检查我的以下代码

package com.selenium.wipro.sample;    

import java.io.File;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxBinary;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class ExampleScanrio {

public static void main(String[] args) throws InterruptedException

    {
     System.setProperty("webdriver.gecko.driver", "C:\\Users\\Yaser\\dist\\geckodriver-master\\geckodriver.exe");
     File pathBinary = new File("C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe");
     FirefoxBinary firefoxbinary = new FirefoxBinary(pathBinary);
     DesiredCapabilities desired = DesiredCapabilities.firefox();
     FirefoxOptions options = new FirefoxOptions();
     desired.setCapability(FirefoxOptions.FIREFOX_OPTIONS, options.setBinary(firefoxbinary));       
     WebDriver driver = new FirefoxDriver(options);
     driver.get("https://mettl.com/corporate/login");
     driver.manage().window().maximize();
     driver.findElement(By.name("userName")).sendKeys("sometext");
     driver.findElement(By.name("password")).sendKeys("sometext");
     driver.findElement(By.xpath("//button[@type='submit']")).click();

     driver.navigate().to("https://mettl.com/corporate/live-feed#/proctoringDashboard");

     WebDriverWait wait = new WebDriverWait(driver,50);
     wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[@class='pull-left']")));

     // Click -> AllTests
     driver.findElement(By.xpath("//span[@class='pull-left']")).click();

     // Click -> Authorize
     driver.findElement(By.xpath("//button[contains(text(),'Authorize')]")).click();

     // Find String     
     String tt = "No more candidates awaiting Authorization";
     String nomore = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");

     // i need condition Statement and Loop Statement here
     // It might be wrong please give a correct Statement

    boolean run = true;

    while(run)
    {
        if (nomore.contentEquals(tt))
            {
            driver.navigate().to("https://mettl.com/corporate/live-feed#/proctoringDashboard");
            //Thread.sleep(750);
            driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();
            driver.findElement(By.xpath("//button[contains(text(),'Authorize')]")).click();
            Thread.sleep(1200);
            System.out.println("Not Available");

            }

        else
            {
            System.out.println("Available");
            //Thread.sleep(10000);
            run = false;
            //break;                                    
            } 

        } 


    }
}

和这样的错误

Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 63.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 20324, moz:profile: C:\Users\Yaser\AppData\Loca..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: 39efeef7-4514-4b83-b31b-deaf937ee098
*** Element info: {Using=xpath, value=//label[@class='checkbox ng-binding']//input[@type='checkbox']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at automation.ExampleScanario.main(ExampleScanario.java:52)

请帮助我解决这个问题,我不是软件工程师,请考虑一下我的英语知识和Java初学者

最后

当候选数据显示在窗口中

将这些日期复制到记事本

Xpath位置

//span[@title='First Name']
//span[@title='Date of Birth']
//span[@title='Email Address']

谢谢

Yaser B

4 个答案:

答案 0 :(得分:0)

我不知道您到底想做什么,但是尝试以下循环:

boolean run = true;

while(run){
  if(statement){
    Do something
  }else{
    Do something else
  }
}

这是一个无限循环;为了避免这种情况,您可以将run设置为false,它将停止。

真诚的席尔文

答案 1 :(得分:0)

它应该像这样工作:

String tt = "No more candidates awaiting Authorization";
String nomore = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");


boolean run = true;

while(run)
{
    if (nomore.contentEquals(tt))
        {
        driver.navigate().to("https://mettl.com/corporate/live-feed#/proctoringDashboard");
        driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();
        driver.findElement(By.xpath("//button[contains(text(),'Authorize')]")).click();
        Thread.sleep(750);
        System.out.println("Not Available");

        }
    else
        {
        System.out.println("Available");
        //Thread.sleep(10000);
        //break;
        run = false; /*Stopps the loop*/                           
        } 

}

答案 2 :(得分:0)

我遇到错误。...

Exception in thread "main" org.openqa.selenium.NoSuchElementException: Unable to locate element: //label[@class='checkbox ng-binding']//input[@type='checkbox']
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:25:53'
System info: host: 'ARAFATH', ip: '192.168.0.56', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: org.openqa.selenium.firefox.FirefoxDriver
Capabilities {acceptInsecureCerts: true, browserName: firefox, browserVersion: 63.0.1, javascriptEnabled: true, moz:accessibilityChecks: false, moz:geckodriverVersion: 0.23.0, moz:headless: false, moz:processID: 18440, moz:profile: C:\Users\Yaser\AppData\Loca..., moz:useNonSpecCompliantPointerOrigin: false, moz:webdriverClick: true, pageLoadStrategy: normal, platform: WINDOWS, platformName: WINDOWS, platformVersion: 10.0, rotatable: false, setWindowRect: true, timeouts: {implicit: 0, pageLoad: 300000, script: 30000}, unhandledPromptBehavior: dismiss and notify}
Session ID: e655d7d3-e0d5-4345-be15-250344290bb4
*** Element info: {Using=xpath, value=//label[@class='checkbox ng-binding']//input[@type='checkbox']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at org.openqa.selenium.remote.service.DriverCommandExecutor.execute(DriverCommandExecutor.java:83)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at automation.ExampleScanario.main(ExampleScanario.java:58)

第58行

    driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();

答案 3 :(得分:-1)

这不是答案

    String tt = "No more candidates awaiting Authorization";
    String nomore = new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@class='ng-scope' and contains(@data-ng-if,'newRequestReceived')]//p[@class='ng-scope' and contains(@data-ng-if,'authRequestCount')]"))).getAttribute("innerHTML");


    boolean run = true;

    do
    {
        if (nomore.contentEquals(tt))
            {
            driver.navigate().to("https://mettl.com/corporate/live-feed#/proctoringDashboard");
            driver.findElement(By.xpath("//label[@class='checkbox ng-binding']//input[@type='checkbox']")).click();
            driver.findElement(By.xpath("//button[contains(text(),'Authorize')]")).click();
            Thread.sleep(750);
            System.out.println("Not Available");

            }

        else
            {
            System.out.println("Available");
            //Thread.sleep(10000);
            break;                                  
            } 

    } while(run);

我的代码是正确的