即使使用正确的Xpath,Webdriver也无法在登录后找到元素

时间:2017-06-14 05:50:32

标签: java selenium selenium-webdriver

Xpath是正确的&网页中没有iFrame,我可以在Xpath检查器中找到元素。但是Webdriver无法定位元素'我的任务'登录后链接。我也使用了wait语句 请帮助在代码或某处找到我的错误.. 这是我的代码..

package onedelta.testAuto;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

//import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import com.relevantcodes.extentreports.ExtentReports;
import com.relevantcodes.extentreports.ExtentTest;
import com.relevantcodes.extentreports.LogStatus;

import org.testng.annotations.Listeners;
import org.testng.annotations.Test;

public class Complius {

    public static WebDriver driver;
    public static Properties p;
    public static FileInputStream f;
    ExtentReports report;
    ExtentTest logger;

    @Test
    public void loginTest() {

        Properties p = new Properties();
        FileInputStream f = null;
        report = new ExtentReports("./Reports/TestReport.html");
        logger = report.startTest("logintest");
        logger.log(LogStatus.INFO, "StartTest");

        try {
            f = new FileInputStream(
                    "C:\\Users\\Himadri\\onedeltaWorkspace\\testAuto\\locators.properties");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            p.load(f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

               // Setup for Firefox Driver
                System.setProperty("webdriver.gecko.driver","C:\\Software\\geckodriver.exe");
                driver = new FirefoxDriver();
                driver.get("https://test.complius.com/");       
                driver.manage().window().maximize();
                WebDriverWait wait2 = new WebDriverWait(driver, 60);
                wait2.until(ExpectedConditions.visibilityOfElementLocated(By.id(p.getProperty("login.username.textfield"))));



                // Login Process Starts here >>>

                try {
                    System.out.println("Enter Login");
                    driver.findElement(By.id(p.getProperty("login.username.textfield"))).sendKeys("something@gmail.com");
                    driver.findElement(By.id(p.getProperty("login.password.textfield"))).sendKeys("Passw0rd");
                    WebElement ele = driver.findElement(By.xpath(p.getProperty("login.signin.button")));
                    ele.sendKeys(Keys.ENTER);
                    /* String classValue = ele.getAttribute("class"); */
                    System.out.println("Login Successful");
                } catch (Exception e) {
                    System.out.println("Login Un-Successful");
                }
    }


    @Test(dependsOnMethods = "loginTest")
    public void searchTest() {

        Properties p = new Properties();
        FileInputStream f = null;
        report = new ExtentReports("./Reports/TestReport.html");
        logger = report.startTest("logintest");
        logger.log(LogStatus.INFO, "StartTest");

        try {
            f = new FileInputStream(
                    "C:\\Users\\Himadri\\onedeltaWorkspace\\testAuto\\locators.properties");
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        try {
            p.load(f);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }


              // Verification of My Tasks- Search Bar >>>
    //          try {
                    System.out.println("Verify Search Bar");
                    WebDriverWait wait3 = new WebDriverWait(driver, 20);
                    wait3.until(ExpectedConditions.elementToBeClickable(By.id(p.getProperty("menu.mytasks.link"))));
                    WebElement mytask = driver.findElement(By.id(p.getProperty("menu.mytasks.link")));
                    mytask.sendKeys(Keys.ENTER);
//                  driver.findElement(By.id(p.getProperty("mytasks.search.textfield"))).sendKeys("Conditions");
//                  driver.findElement(By.id(p.getProperty("mytasks.search.button"))).click();
                    System.out.println("Search Successful");
//              } catch (Exception e) {
                    System.out.println("Search Un-Successful");
//              }






     }

}

1 个答案:

答案 0 :(得分:0)

我已经检查了网站,下面的xpath可以在主页上单击“我的任务”。

 driver.findElement(By.xpath("//a[contains(text(),'My Tasks')]")).click();

启动驱动程序后,还要添加一些隐式等待。

driver.get("https://test.complius.com/");       
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

希望这会对你有所帮助。感谢。

完整代码:

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

    String Chromedriverpath = "/home/santhoshkumar/Softwares/Selenium/drivers/chromedriver2.29";        
    System.setProperty("webdriver.chrome.driver", Chromedriverpath);
    WebDriver driver=new ChromeDriver();
     driver.get("https://test.complius.com/");       
     driver.manage().window().maximize();
     driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

     System.out.println("Enter Login");
     driver.findElement(By.id("auth_user_email")).sendKeys("XXXX@gmail.com");
     driver.findElement(By.id("auth_user_password")).sendKeys("XXXX");
     driver.findElement(By.xpath("//*[@id='loginform']/div[4]/span[2]/input")).click();
     driver.findElement(By.xpath("//a[contains(text(),'My Tasks')]")).click();

}