【问题标题】:I am not able to find elements in selenium [duplicate]我无法在硒中找到元素 [重复]
【发布时间】:2019-04-01 00:17:37
【问题描述】:

我正在尝试运行以下 selenium 代码,但出现异常:

包演示;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class AmazonLogin {

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


        WebDriver driver;
        // Go to website and login


        driver = utilites.DriverFactor.open("chrome");
        driver.get("https://www.amazon.in/your-account");
        WebElement loginName = driver.findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div[2]/a/div/div"));
        WebElement emailId = driver.findElement(By.xpath("//*[@id=\"ap_email\"]"));
        //  WebElement continueButton=driver.findElement(By.id("continue"));
        //  WebElement password=driver.findElement(By.name("password"));
        //  WebElement loginButton=driver.findElement(By.id("signInSubmit"));
        //  WebElement message=driver.findElement(By.className("nav-line-1"));
        //
        loginName.click();
        emailId.sendKeys("aryan.ragavan@gmail.com");
    }
}

线程“主”org.openqa.selenium.NoSuchElementException 中的异常:没有这样的 元素:
无法定位元素:{"method":"xpath","selector":"//*[@id="ap_email"]"}

Selenium 在单击 loginName webelement 之前尝试查找 webelement emailid。请帮忙。

【问题讨论】:

    标签: java selenium selenium-webdriver


    【解决方案1】:

    这是因为您在单击登录之前尝试读取 emailId 字段。将以下行移到loginName.click() 语句下方。

        WebElement emailId = driver.findElement(By.xpath("//*[@id=\"ap_email\"]"));
    

    【讨论】:

      【解决方案2】:

      问题是您在亚马逊主页上搜索“app_element”。

      在此处介绍POM实现:

      https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html

      请勿在任何情况下都使用绝对 xpath,就像您在此处所做的那样:

      findElement(By.xpath("/html/body/div[1]/div[2]/div/div[2]/div[2]/a/div/div"));

      这是亚马逊登录逻辑的示例:

      @Test
      public void amazon_login() {
          browseToUrl("https://www.amazon.in/your-account");
      
          // select login & security option
          WebElement loginAndSecurityBtn = driver.findElement(By.xpath("//div[@data-card-identifier='SignInAndSecurity']"));
          // navigate to the next page
          loginAndSecurityBtn.click();
      
          // enter email or phone
          WebElement emailOrPhoneInput = driver.findElement(By.id("ap_email"));
          emailOrPhoneInput.sendKeys("example@email.com");
      
          // click continue btn
          WebElement continueBtn = driver.findElement(By.id("continue"));
          continueBtn.click();
      
          // enter password
          WebElement passwordInput = driver.findElement(By.id("ap_password"));
          passwordInput.sendKeys("password");
      
          // login
          WebElement loginBtn = driver.findElement(By.id("signInSubmit"));
          loginBtn.click();
      }
      

      【讨论】:

        【解决方案3】:

        对于亚马逊登录,您可以参考此代码。或低于 POM(PageobjectModel) 代码

            package TestId;
        
        import org.openqa.selenium.By;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.chrome.ChromeDriver;
        import org.openqa.selenium.interactions.Actions;
        import org.openqa.selenium.support.ui.ExpectedCondition;
        import org.openqa.selenium.support.ui.ExpectedConditions;
        import org.openqa.selenium.support.ui.WebDriverWait;
        import org.testng.annotations.AfterTest;
        import org.testng.annotations.Test;
        
        public class NewTest {
        
            WebDriver driver;
          @Test
          public void f() 
          {
              System.setProperty("webdriver.chrome.driver", "E:\\New Folder\\chromedriver.exe");
        
              driver = new ChromeDriver();
        
              driver.manage().window().maximize();
        
              driver.get("https://www.amazon.in/your-account");
        
              WebElement e1 = driver.findElement(By.xpath("//*[@id='nav-link-yourAccount']/span[2]"));
              WebElement e2 = driver.findElement(By.xpath("//*[@id='nav-flyout-ya-signin']/a/span"));
              Actions a1 = new Actions(driver);
              a1.moveToElement(e1).click(e2).build().perform();
          }
        
          @AfterTest
          public void CheckLogin()
          {
        
              WebElement e3 = driver.findElement(By.xpath("//*[@id='authportal-main-section']/div[2]/div/div[1]/form/div/div/div"));
        
              WebDriverWait wait = new WebDriverWait(driver, 5);
              WebElement e4 = wait.until(ExpectedConditions.visibilityOf(e3));
        
              if(e4.isDisplayed())
              {
                  driver.findElement(By.id("ap_email")).sendKeys("Test@gmail.com");
        
                  System.out.println("Email Successfully Passed");
              } 
          }
        }
        

        我使用 Webdriver 等待是因为,我需要等到登录页面被加载,并且我使用了 if 条件,因为在显示页面后传递电子邮件 ID。

        如果您在上述使用中发现困难,请使用以下 POM 方法。

        使用 POM 登录亚马逊:

            package POM;
        
        import org.apache.xmlbeans.impl.xb.xsdschema.Public;
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.WebElement;
        import org.openqa.selenium.interactions.Actions;
        import org.openqa.selenium.support.FindBy;
        import org.openqa.selenium.support.How;
        import org.openqa.selenium.support.ui.ExpectedConditions;
        import org.openqa.selenium.support.ui.WebDriverWait;
        
        public class Pageobject 
        {
            WebDriver driver;
        
            public Pageobject(WebDriver driver)
            {
                this.driver=driver;
            }
        
            @FindBy(xpath = "//*[@id='nav-link-yourAccount']/span[2]")
            public WebElement Siginpath;
        
            @FindBy(how=How.XPATH,using="//*[@id='nav-flyout-ya-signin']/a/span")
            public WebElement clicksignin;
        
            @FindBy(how=How.XPATH,using="//*[@id='authportal-main-section']/div[2]/div/div[1]/form/div/div/div")
            public WebElement ElementtobeVisible;
        
            @FindBy(how=How.ID,using="ap_email")
            public WebElement Email;
        
        
            public void Loginpage(String email)
            {
                Actions a1 = new Actions(driver);
                  a1.moveToElement(Siginpath).click(clicksignin).build().perform();
        
                  WebDriverWait wait = new WebDriverWait(driver, 5);
                  WebElement e4 = wait.until(ExpectedConditions.visibilityOf(ElementtobeVisible));
        
                  if(e4.isDisplayed())
                  {
                      Email.sendKeys(email);
                  }  
            }
        
        }
        

        POM 测试用例:

            package POMtestcase;
        
        import org.openqa.selenium.WebDriver;
        import org.openqa.selenium.chrome.ChromeDriver;
        import org.openqa.selenium.support.PageFactory;
        import org.testng.annotations.Test;
        
        import POM.Pageobject;
        
        public class Pomtest 
        {
            WebDriver driver;
        
            @Test
            public void Checkvaliduser()
            {
                System.setProperty("webdriver.chrome.driver", "E:\\New Folder\\chromedriver.exe");
        
                  driver = new ChromeDriver();
        
                  driver.manage().window().maximize();
        
                  driver.get("https://www.amazon.in/your-account");
        
                 Pageobject object = PageFactory.initElements(driver, Pageobject.class); //Calling The Class Here
        
                 object.Loginpage("test@gmail.com");//Passing the Mail id
        
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-01-23
          • 2016-10-07
          • 2021-08-12
          • 2019-05-05
          • 2021-08-15
          • 1970-01-01
          相关资源
          最近更新 更多