我正在尝试对创建 facebook 帐户进行自动化测试

问题描述

我正在尝试通过创建帐户来在 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']

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...