如何使用Selenium Webdriver正确查找网页元素?

时间:2014-02-18 10:02:22

标签: java facebook selenium

我在Facebook上练习,

WebDriver driver = new HtmlUnitDriver();


driver.get("http://www.facebook.com");

成功登录后,我尝试发送消息。但我找不到消息区域:

WebElement selectMessages = driver.findElement(By.id("fbMessagesFlyout"));
        selectMessages.click();


        WebElement elementMess = driver.findElement(By.id("q"));
        elementMess.sendKeys("Dalia");
        elementMess.submit();

此代码只是在标题上部署带有消息的javascript,然后在fb标头处发送搜索请求。 如何找到拨号页面的正确链接,然后找到文本咏叹调?

1 个答案:

答案 0 :(得分:1)

这个工作!!

@BeforeTest
public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "https://www.facebook.com";
    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}

@Test
public void testUntitled() throws Exception {
    driver.get(baseUrl + "/login.php");
    driver.findElement(By.id("email")).sendKeys("youridhere@gmail.com");
    driver.findElement(By.id("pass")).sendKeys("yourpassword");
    driver.findElement(By.name("login")).click();
    Thread.sleep(3000);
    WebElement msgframe = driver.findElement(By.id("fbMessagesJewel"));
    msgframe.findElement(By.tagName("a")).click();
    List<WebElement> element = driver.findElements(By
            .className("messagesContent"));
    element.get(0).click();
    WebElement chat = driver.findElement(By.className("fbNubFlyoutFooter"));
    chat.findElement(By.tagName("textarea")).sendKeys("Hi");
    driver.findElement(By.tagName("textarea")).sendKeys(Keys.RETURN);
    Thread.sleep(2000);
}