简体   繁体   English

Selenium + Java-在页面中找不到WebElements

[英]Selenium+Java - Unable to find WebElements in page

I'm doing some automation bits for work and right now I'm trying to automate a purchase through our storefront that should go to the paypal sandbox and complete the purchase. 我正在做一些自动化工作,现在我正在尝试通过店面自动进行购买,该购买应转到Paypal沙箱并完成购买。 Everything looks good and I know the general flow works but I'm having trouble finding the webElements when I get to the first PayPal page. 一切看起来都不错,我知道总体流程可行,但是当我进入第一个PayPal页面时,我很难找到webElements。

The PayPal side of the flow consists of 2 pages. 该流程的PayPal一侧包含2页。 One to input the login information and another one to confirm the purchase. 一个输入登录信息,另一个输入确认购买。 The second page works perfectly but the first one always gives me "Unable to find element" when I tell it to look for the email/password field and the login button. 第二页工作正常,但是当我告诉它查找电子邮件/密码字段和登录按钮时,第一页始终给我“无法找到元素”。 If make the driver print out the current URL for debugging purposes it correctly prints the payPal URL so it is looking at the right site. 如果使驱动程序打印出当前URL以进行调试,则它会正确打印出payPal URL,因此它在寻找正确的站点。 I also tried putting a 30 seconds delay to make sure it wasn't a timing issue and I get the same problem. 我还尝试将延迟时间设置为30秒,以确保这不是时间问题,并且我遇到了同样的问题。

Here's the class file in question: 这是有问题的课程文件:

public class PayPalLoginPage extends AbstractPaymentPage {
    //AbstractPaymentPage extends from AbrstractPageObject

      private WebElement email; //Element ID is email
      private WebElement password; //Element ID is password
      @FindBy(id = "btnLogin")
      private WebElement loginButton;

      public PayPalLoginPage(WebDriver driver) {
        super(driver);
        PageFactory.initElements(driver, this);
      }


      public PayPalPurchaseConfirmationPage login (PayPalInfo payPalInfo) {
        email.sendKeys(payPalInfo.getEmail());
        password.sendKeys(payPalInfo.getPassword());
        loginButton.click();
        this.waitForPayPalLoadingCurtainToDisappear();
        return new PayPalPurchaseConfirmationPage(this.getDriver());
      }

The way I'm calling this class is like this: 我调用此类的方式是这样的:

case PAYPAL_PURCHASE:
  setDriver(new PayPalLoginPage(getDriver()).login(getPaymentMethod()).confirmPayPalPurchase());
  break;

So, the flow works perfectly up until it gets to the first payPal page and it just stops saying it can't find any of those 3 elements. 因此,该流程可以完美运行直到到达第一个payPal页面,并且只是停止说找不到这3个元素中的任何一个。 If I set it to just wait there and manually fill up the information then it picks right up on the next page and works from them on (finding all the elements on the 2nd payPal page and acting on them). 如果我将其设置为仅在此处等待并手动填写信息,则它将在下一页上进行选择并在它们上进行操作(在第二个payPal页面上查找所有元素并对其进行操作)。

I get the same behavior if I put the findElement.By line inside the login method and also the same result regardless of whether I'm trying to find them using id, name, xpath or css. 如果将findElement.By行放置在login方法内,则得到相同的行为,并且无论我是否尝试使用id,name,xpath或css查找它们,结果都相同。

Any idea on what I could be missing?. 关于我可能会想念的任何想法吗?

You are just defining the email and followings as WebElement but not assigning them to any webelement. 您只是将电子邮件和后续邮件定义为WebElement,而不是将其分配给任何Webelement。 See the following: 请参阅以下内容:

private WebElement email = driver.findElement(By.id("email_text_box_id"));
private WebElement password = driver.findElement(By.id("password_text_box_id"));
private WebElement loginbutton = driver.findElement(By.id("login_button_id"));

if you want PageObject to assign WebElements then you need to call the initElement inside of a case: 如果要PageObject分配WebElement,则需要在initElement内部调用initElement

public class PayPalLoginPage extends AbstractPaymentPage {
//AbstractPaymentPage extends from AbrstractPageObject

  private WebElement email; //Element ID is email
  private WebElement password; //Element ID is password
  @FindBy(id = "btnLogin")
  private WebElement loginButton;

  public PayPalPurchaseConfirmationPage login (PayPalInfo payPalInfo) {
    email.sendKeys(payPalInfo.getEmail());
    password.sendKeys(payPalInfo.getPassword());
    loginButton.click();
    this.waitForPayPalLoadingCurtainToDisappear();
    return new PayPalPurchaseConfirmationPage(this.getDriver());
  }
}

Call the PayPal login function: 调用PayPal登录功能:

public class UsingPayPalLoginPage {
    public static void main(String[] args) {
      // Create a new instance of a driver
      WebDriver driver = new HtmlUnitDriver();

      // Create a new instance of the PayPall page
      // and initialise any WebElement fields in it.
      PayPalLoginPage page = PageFactory.initElements(driver, PayPalLoginPage.class);

      // call login of paypal.
      page.login(payPalInfo);
  }
} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM