问题描述
我正在尝试通过创建帐户来在 Facebook 上进行自动化测试,但是我在单击“创建帐户”按钮时遇到问题,我正在尝试通过 id 使用 FindElement,但是它不工作。
public void criarConta(String nome,String apelido,String email,String password) {
driver.findElement(By.id("u_0_2_OE")).click();
WebElement nomeElement = driver.findElement(By.name("firstname"));
nomeElement.sendKeys("Teste");
WebElement apelidoElement = driver.findElement(By.name("lastname"));
apelidoElement.sendKeys("Alfasoft");
WebElement emailElement = driver.findElement(By.name("reg_email__"));
emailElement.sendKeys("fabio.teste@alfasoft.pt");
WebElement passwordElement = driver.findElement(By.name("reg_passwd__"));
passwordElement.sendKeys("AlfasoftTeste_");
driver.findElement(By.name("birthday_day")).click();
WebElement idElement = driver.findElement(By.id("day"));
idElement.sendKeys("6");
driver.findElement(By.name("birthday_month")).click();
WebElement mesElement = driver.findElement(By.id("month"));
mesElement.sendKeys("Ago");
driver.findElement(By.name("birthday_year")).click();
WebElement anoElement = driver.findElement(By.id("year"));
anoElement.sendKeys("1999");
driver.findElement(By.id("u_h_3_BO")).click();
driver.findElement(By.name("websubmit")).click();
}
(我在另一个类中调用此代码使用:public static void main(String[] args)
throws Exception 然后 tarefa2Project.criarConta("nome","apelido","email","password");
...
解决方法
您可以在此处使用 xpath,使用元素属性和文本使其独一无二:
driver.findElement(By.xpath("//a[@role='button'][text()='Create New Account']")).click();
,
为了不绑定到元素的文本并且不使用 xpath
,您可以使用以下 css
选择器:li>a[role='button']