Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/376.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
Java Selenium自动元素单击返回null_Java_Selenium_Selenium Webdriver_Testng - Fatal编程技术网

Java Selenium自动元素单击返回null

Java Selenium自动元素单击返回null,java,selenium,selenium-webdriver,testng,Java,Selenium,Selenium Webdriver,Testng,我已经用selenium编写了一个自动测试用例来测试登录页面,应该单击忘记密码链接。浏览器打开并转到给定的url,但忘记密码链接不会自动单击,有人能告诉我的代码有什么问题吗 基本代码 public class Forgetpsw { WebDriver driver; @BeforeTest public void Browseropen() throws Exception { Browsers startup=new Browsers();

我已经用selenium编写了一个自动测试用例来测试登录页面,应该单击忘记密码链接。浏览器打开并转到给定的url,但忘记密码链接不会自动单击,有人能告诉我的代码有什么问题吗

基本代码

public class Forgetpsw {

    WebDriver driver;

    @BeforeTest
    public void Browseropen() throws Exception {
        Browsers startup=new  Browsers();
        startup.Initiating();
    }

    @Test
    public void Forget(String username) throws Exception{
        driver.findElement(By.xpath("//*[text()='Forgot Password?']")).click();
        Thread.sleep(3000L);
        driver.findElement(By.xpath("//*[@placeholder='User ID'][@id='forgotPasswordForm:j_idt13:0:username']")).sendKeys(username);
    }
}
Browsers.java

public class Browsers {
    public  WebDriver driver;

    public void Initiating() throws IOException {
        Properties prop= new Properties();
        InputStream fis = new FileInputStream("D:\\Workspace\\TMS\\selenium\\ZLive\\src\\datadriven.properties");
        prop.load(fis);


        System.out.println("Initiating Browser");

        if (prop.getProperty("browser").equals("firefox"))
        {
            driver=new FirefoxDriver();
        }
        else
        {
            System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver_win32\\chromedriver.exe");
            driver = new ChromeDriver();
        }
        driver.manage().window().maximize();
        driver.get(prop.getProperty("url"));
    }
}
数据属性

browser=firefox
url=http://abcd.xxx.in/login
username=32131
pasword=32131
控制台:

[TestNG] Running:
  C:\Users\ds\AppData\Local\Temp\testng-eclipse-42991103\testng-customsuite.xml

Initiating Browser

===============================================
    Default test
    Tests run: 0, Failures: 0, Skips: 0
===============================================


===============================================
Default suite
Total tests run: 0, Failures: 0, Skips: 0
===============================================
HTML代码

<div class="loginRelative" id="secondDiv"><label class="loginText">Transport Management System</label>
          <div class="logo-div">
            <div class="loginimageBgStyle"><img src="images/indexImg.png" /></div>
          </div>
          <div class="absoluteLeft"></div>
          <div class="absoluteRight"></div><span id="loginForm:loginPanel"><div id="loginForm:messages" class="ui-messages ui-widget errorValidationStyle" aria-live="polite"></div>

          <div class="controls loginTextbox" id="thirdDiv"><div id="loginForm:j_idt16:0:j_idt18" class="ui-outputpanel ui-widget"><input id="loginForm:j_idt16:0:username" name="loginForm:j_idt16:0:username" type="text" placeholder="User ID" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" /><script id="loginForm:j_idt16:0:username_s" type="text/javascript">PrimeFaces.cw("InputText","widget_loginForm_j_idt16_0_username",{id:"loginForm:j_idt16:0:username",widgetVar:"widget_loginForm_j_idt16_0_username"});</script></div>
          </div><div id="loginForm:j_idt16:0:j_idt20" aria-live="polite" class="ui-message ui-helper-hidden"></div><script id="loginForm:j_idt16:0:j_idt20_s" type="text/javascript">$(function(){PrimeFaces.cw("Message","widget_loginForm_j_idt16_0_j_idt20",{id:"loginForm:j_idt16:0:j_idt20",widgetVar:"widget_loginForm_j_idt16_0_j_idt20",target:"loginForm:j_idt16:0:username"});});</script>  

          <div class="controls loginPassword"><div id="loginForm:j_idt16:0:j_idt22" class="ui-outputpanel ui-widget"><input id="loginForm:j_idt16:0:password" name="loginForm:j_idt16:0:password" type="password" class="ui-inputfield ui-password ui-widget ui-state-default ui-corner-all" placeholder="******" /><script id="loginForm:j_idt16:0:password_s" type="text/javascript">$(function(){PrimeFaces.cw("Password","widget_loginForm_j_idt16_0_password",{id:"loginForm:j_idt16:0:password",widgetVar:"widget_loginForm_j_idt16_0_password"});});</script></div>
                            <!-- &lt;p:keyboard id="" value="" &gt;

                &lt;/p:keyboard&gt; -->
                        </div><div id="loginForm:j_idt16:0:j_idt24" aria-live="polite" class="ui-message ui-helper-hidden"></div><script id="loginForm:j_idt16:0:j_idt24_s" type="text/javascript">$(function(){PrimeFaces.cw("Message","widget_loginForm_j_idt16_0_j_idt24",{id:"loginForm:j_idt16:0:j_idt24",widgetVar:"widget_loginForm_j_idt16_0_j_idt24",target:"loginForm:j_idt16:0:password"});});</script>

          <div><a href="/forgotPassword" class="forgotPwdStyle">Forgot Password?</a>
          </div>

          <div class="loginBtnBgStyle"><button id="loginForm:j_idt28" name="loginForm:j_idt28" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-icon-only noBtnStyle" type="button"><span class="ui-button-icon-left ui-icon ui-c loginBtnImg"></span><span class="ui-button-text ui-c">ui-button</span></button><script id="loginForm:j_idt28_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_loginForm_j_idt28",{id:"loginForm:j_idt28",widgetVar:"widget_loginForm_j_idt28"});</script><button id="loginForm:loginButton" name="loginForm:loginButton" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only loginBtnStyle" onclick="return loginTms();;" type="button"><span class="ui-button-text ui-c">Login</span></button><script id="loginForm:loginButton_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_loginForm_loginButton",{id:"loginForm:loginButton",widgetVar:"widget_loginForm_loginButton"});</script>
          </div></span><script type="text/javascript">loginTms = function() {PrimeFaces.ab({s:'loginForm:j_idt30',f:'loginForm',u:'loginForm',pa:arguments[0]});}</script>

          <script>
           function TMS_onLoginBtnClick(){

            //alert("localhostinnnn");
            var payroll = $("#payrollIframe").contents();
            payroll.find("#username").val(document.getElementById("loginForm:j_idt15:0:username").value);
            payroll.find("#password").val(document.getElementById("loginForm:j_idt15:0:password").value);
            payroll.find("#user_accounts")[0].submit();
            return false;
           }


           /**
            This method will be called from payroll 
            iframe after success full login
           */
           function loginSuccess(){
              //alert("logged in");
            //TODO: handle here
              loginTms();
           }

           function loginFail(){

            //TODO: handle here
           }
        </script>
        <iframe id="payrollIframe" src="http://localhost/GOPayroll/?source=1" style="opacity:0; height:1px; width:1px;">
        </iframe>

          <div class="logoutMsgStyle">

          </div>


    </div>
    </div>
运输管理系统
cw(“InputText”,“widget\u loginForm\u j\u idt16\u 0\u username”,“id:“loginForm:j\u idt16:0:username”,widgetVar:“widget\u loginForm\u j\u idt16\u 0\u username”});
$(function(){PrimeFaces.cw(“Message”,“widget_loginForm_idt16_0_idt20”,“id:“loginForm:j_idt16:0:j_idt20”,widgetVar:“widget_loginForm_idt16_0_idt20”,target:“loginForm:j_idt16:0:username”});
$(function(){PrimeFaces.cw(“密码”,“小部件”登录信息表格“\u ju idt16\u 0\u密码“,{id:“登录信息表格:ju idt16:0:Password”,widgetVar:“小部件”\u loginForm\u ju idt16\u 0\u密码”});
$(function(){PrimeFaces.cw(“Message”,“widget_loginForm_idt16_0_idt24”,“id:“loginForm:j_idt16:0:j_idt24”,widgetVar:“widget_loginForm_idt16_0_idt24”,target:“loginForm:j_idt16:0:0:password”});
ui buttonPrimeFaces.cw(“CommandButton”、“widget_loginForm_j_idt28”、{id:“loginForm:j_idt28”、widgetVar:“widget_loginForm_idt28”});cw(“CommandButton”、“widget\u loginForm\u loginButton”、{id:“loginForm:loginButton”、widgetVar:“widget\u loginForm\u loginButton”});
loginTms=function(){PrimeFaces.ab({s:'loginForm:j_idt30',f:'loginForm',u:'loginForm',pa:arguments[0]});}
函数TMS_onLoginBtnClick(){
//警报(“localhostinnn”);
var payroll=$(“#payrollIframe”).contents();
payroll.find(“#username”).val(document.getElementById(“loginForm:j#uidt15:0:username”).value);
payroll.find(“#password”).val(document.getElementById(“loginForm:j#uidt15:0:password”).value);
payroll.find(“#用户_帐户”)[0].submit();
返回false;
}
/**
此方法将从工资单中调用
成功完全登录后的iframe
*/
函数loginsAccess(){
//警报(“登录”);
//TODO:在这里处理
loginTms();
}
函数loginFail(){
//TODO:在这里处理
}

初始化方法中,您需要返回驱动程序对象并将其存储在您的Forgetpsw类中

public void Initiating() throws IOException {
    Properties prop= new Properties();
    InputStream fis = new FileInputStream("D:\\Workspace\\TMS\\selenium\\ZLive\\src\\datadriven.properties");
    prop.load(fis);


    System.out.println("Initiating Browser");

    if (prop.getProperty("browser").equals("firefox"))
    {
        driver=new FirefoxDriver();
    }
    else
    {
        System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    driver.manage().window().maximize();
    driver.get(prop.getProperty("url"));
    return driver;   //return the driver object.
 }
browservepen
方法如下所示

  public void Browseropen() throws Exception {
    Browsers startup=new  Browsers();
    driver = startup.Initiating();
  }

初始化方法中,您需要返回驱动程序对象并将其存储在Forgetpsw类中

public void Initiating() throws IOException {
    Properties prop= new Properties();
    InputStream fis = new FileInputStream("D:\\Workspace\\TMS\\selenium\\ZLive\\src\\datadriven.properties");
    prop.load(fis);


    System.out.println("Initiating Browser");

    if (prop.getProperty("browser").equals("firefox"))
    {
        driver=new FirefoxDriver();
    }
    else
    {
        System.setProperty("webdriver.chrome.driver", "D:\\selenium\\chromedriver_win32\\chromedriver.exe");
        driver = new ChromeDriver();
    }
    driver.manage().window().maximize();
    driver.get(prop.getProperty("url"));
    return driver;   //return the driver object.
 }
browservepen
方法如下所示

  public void Browseropen() throws Exception {
    Browsers startup=new  Browsers();
    driver = startup.Initiating();
  }
在initialing()方法中,必须返回驱动程序,如下所示

public WebDriver Initiating() throws IOException { 
    //your code
    return driver
}
并在中使用此返回的驱动程序

@BeforeTest
public void Browseropen() throws Exception {
    Browsers startup=new  Browsers();
    this.driver = startup.Initiating(); //your declared driver will get the initialized driver.
}
希望这将对您有所帮助。

在您的initialing()方法中,您必须像返回驱动程序一样返回驱动程序

public WebDriver Initiating() throws IOException { 
    //your code
    return driver
}
并在中使用此返回的驱动程序

@BeforeTest
public void Browseropen() throws Exception {
    Browsers startup=new  Browsers();
    this.driver = startup.Initiating(); //your declared driver will get the initialized driver.
}

希望这能对您有所帮助。

您遇到了什么样的例外情况?另外,你能提供你的HTML吗?我没有得到错误,但也没有得到想要的输出。首先,你在两个类
Forgetpsw
浏览器中创建
WebDriver
。执行
Browsers
类时,其
Webdriver
打开您的URL。然后将控件返回到
Forgetpsw
类,其中
WebDriver
尚未启动或分配。在这种情况下,它不会单击任何内容。考虑将<代码> WebDriver <代码>传递给您的<代码>初始化()>代码>方法。我发现问题而不是Or.TestNg.Annutux.TestI导入Or.JunIT.Test.我正在调试testng用例,所以测试方法并没有调用,你们得到了什么样的异常?另外,你能提供你的HTML吗?我没有得到错误,但也没有得到想要的输出。首先,你在两个类
Forgetpsw
浏览器中创建
WebDriver
。执行
Browsers
类时,其
Webdriver
打开您的URL。然后将控件返回到
Forgetpsw
类,其中
WebDriver
尚未启动或分配。在这种情况下,它不会单击任何内容。考虑将<代码> WebDriver <代码>传递给您的<代码>初始化()>代码>方法。我发现问题而不是Or.TestNg.Annutux.TestI导入Or.JunIT.Test.我正在调试testng用例,所以测试方法并没有调用