如何获取此代码中的文字

时间:2016-06-10 17:07:00

标签: selenium selenium-webdriver webdriver

在下面的代码中,代码可用。请检查一下。文字无法打印。

WebDriver driver = new FirefoxDriver ();
driver.navigate().to("https://www.facebook.com");
driver.manage().window().maximize();
driver.findElement(By.id("u_0_1")).sendKeys("surya4u");
driver.findElement(By.id("u_0_3")).sendKeys("surnamerajesh4");
driver.findElement(By.id("u_0_6")).sendKeys("surya4u@yopmail.com");
driver.findElement(By.id("u_0_9")).sendKeys("surya4u@yopmail.com");
driver.findElement(By.id("u_0_b")).sendKeys("surya4u");
Select date = new Select(driver.findElement(By.id("day")));
date.selectByValue("24");
Select month = new Select(driver.findElement(By.id("month")));
month.selectByValue("9");
Select year = new Select(driver.findElement(By.id("year")));
year.selectByValue("1987");
driver.findElement(By.cssSelector("#u_0_f")).click();
driver.findElement(By.id("u_0_j")).click();
WebElement text = driver.findElement(By.xpath(".//*[@id='reg_error_inner']"));
System.out.println(text.getText());
driver.close();

1 个答案:

答案 0 :(得分:0)

您需要等待错误消息可见。我添加了一个等待并更新了错误文本的选择器。

WebDriver driver = new FirefoxDriver();
driver.navigate().to("https://www.facebook.com");
driver.manage().window().maximize();
driver.findElement(By.id("u_0_1")).sendKeys("surya4u");
driver.findElement(By.id("u_0_3")).sendKeys("surnamerajesh4");
driver.findElement(By.id("u_0_6")).sendKeys("surya4u@yopmail.com");
driver.findElement(By.id("u_0_9")).sendKeys("surya4u@yopmail.com");
driver.findElement(By.id("u_0_b")).sendKeys("surya4u");
Select date = new Select(driver.findElement(By.id("day")));
date.selectByValue("24");
Select month = new Select(driver.findElement(By.id("month")));
month.selectByValue("9");
Select year = new Select(driver.findElement(By.id("year")));
year.selectByValue("1987");
driver.findElement(By.cssSelector("#u_0_f")).click();
driver.findElement(By.id("u_0_j")).click();
WebElement text = driver.findElement(By.id("reg_error_inner"));

// Wait for text to be visible
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.visibilityOf(text));

System.out.println(text.getText());
driver.close();