【问题标题】:Can't find an element in the browser在浏览器中找不到元素
【发布时间】:2016-02-12 22:23:08
【问题描述】:

我正在使用 Selenium、Java、Cucumber 构建一个自动化项目。

此时我有 3 个不同的类在运行自动化代码 - 容器类:将所有 WebElement 存储为单个方法 - 查看类:所有逻辑发生的地方。 exp:对存储在容器类中的 Webelement 执行点击。 -Steps 类:我在其中断言或验证场景的不同部分

我的问题是当我从视图类调用容器方法时,我没有在网页端找到 webelements...

我知道路径没问题,因为如果我在步骤类中构建整个场景,所有 WebElement 都可以毫无问题地定位

我知道视图类正在执行容器类,因为我在容器方法中设置了一个 prinln,并且每次视图类尝试查找 WebElement 时都会显示。

容器类

 package com.automation.automation.prototype.containers;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class loginContainer {

    private WebDriver driver;

    public loginContainer(WebDriver driver) {
        this.driver = driver;

    }

    public WebElement loginEmail(){

        //xpath that is not beign located
        return driver.findElement(By.xpath("//*[@id='email']"));

    }
}

查看类

package com.automation.automation.prototype.views;
import com.automation.automation.prototype.containers.loginContainer;
import org.openqa.selenium.WebDriver;

import com.automation.automation.prototype.containers.loginContainer;

public class loginView {

private static loginContainer login;


    public loginView(WebDriver driver) {

        login = new loginContainer(driver);
        }

    public boolean insertEmail( String email) throws InterruptedException{

        boolean valid = false; 
        int flag = 0;
        int attemps = 0; 

        do{
            attemps++;

            try{
                login.loginEmail().click();
                login.loginEmail().sendKeys(email);

                System.out.println("Element Found!: "+attemps);
            valid = true;
            flag = 1;
            }
            catch(Exception e){
                Thread.sleep(1000);
            System.out.println("Element Searching: "+attemps);
            }
    }while (attemps <20 && flag ==0);
            return valid;}
}

步类

 package com.automation.automation.prototype;
    import org.openqa.selenium.By;
    import org.openqa.selenium.WebDriver;
    import org.openqa.selenium.firefox.FirefoxDriver;
    import static org.junit.Assert.assertTrue;

    import com.automation.automation.prototype.containers.URLs;
    import com.automation.automation.prototype.containers.loginContainer;
    import com.automation.automation.prototype.views.loginView;

    import cucumber.api.java.en.Given;
    import cucumber.api.java.en.Then;
    import cucumber.api.java.en.When;

    public class steps {

        private  WebDriver driver;
        private static URLs URL;

        private static loginView LoginView2;
        //private static loginContainer LoginContainerstep;


        public steps(){

            URL = new URLs(driver);
            //LoginContainerstep = new loginContainer(driver);
            LoginView2 = new loginView(driver);

        }
        @Given(value = "^Launch$")
        public void login() throws Throwable{

            driver = new FirefoxDriver();

            driver.get(URL.fbURL);

            System.out.println(URL.fbURL);

        }

            @Then(value = "^insertCredentials (.*)$")
            public void insertCredentials_and_and(String email) throws InterruptedException{

                assertTrue("ELEMENT IS NOT SHOWING UP", LoginView2.insertEmail(email));

                }

【问题讨论】:

  • 您面临什么异常。提供那个
  • 你真的应该尽量减少代码以重现失败+提供异常
  • 我真的很好奇为什么你在构造函数中创建LoginView2 = new loginView(driver) ,但你在Given方法中创建driver = new FirefoxDriver()。您应该在调用之前创建驱动程序。
  • 提供异常堆栈跟踪。

标签: java object selenium automated-tests


【解决方案1】:

尽量减少代码。最好做一个公共类,你可以在其中放置登录,设置和拆卸功能,并使用主类(包含主要逻辑并调用公共所需的函数)。其次,尽量注意访问修饰符。第三,尝试检查可以定义不同类型的 XPATH。希望它会有所帮助。

【讨论】:

    【解决方案2】:
    Try to use this : instead of a retry 
    
    public WebElement findElementSafe(By selector, long timeOutInSeconds) {
            try {
                return findElement(selector, timeOutInSeconds);
            } catch (TimeoutException e) {
                return null;
            }
        }
    
    public WebElement findElement(By selector, long timeOutInSeconds) {
            WebDriverWait wait = new WebDriverWait(driver, timeOutInSeconds);
            wait.until(ExpectedConditions.presenceOfElementLocated(selector));
            return findElement(driver, selector);
        }
    
    
    public void waitForElementToAppear(By selector, long timeOutInSeconds) {
            WebDriverWait wait = new WebDriverWait(getDriver(), timeOutInSeconds);
            wait.until(ExpectedConditions.visibilityOfElementLocated(selector));
        }
    
        public void waitForElementToDisappear(By selector, long timeOutInSeconds) {
            WebDriverWait wait = new WebDriverWait(getDriver(), timeOutInSeconds);
            wait.until(ExpectedConditions.invisibilityOfElementLocated(selector));
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-11
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-09
      相关资源
      最近更新 更多