我正在尝试运行以下硒代码,但出现异常:
软件包演示;
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。请帮忙。
答案 0 :(得分:0)
这是因为您尝试在单击登录名之前先阅读emailId字段。将以下行移动到loginName.click()
语句下方。
WebElement emailId = driver.findElement(By.xpath("//*[@id=\"ap_email\"]"));
答案 1 :(得分:0)
问题是您正在亚马逊主页上搜索“ 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();
}
答案 2 :(得分:0)
对于Amazon登录,您可以参考此代码。或以下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的Amazon登录:
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
}
}