java Selenium 代码打开 Facebook Messenger 并发送消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31168221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 18:13:04  来源:igfitidea点击:

Selenium code to open facebook messenger and send a message

javafacebookselenium

提问by needhelpwithR

I'm just starting to learn a little bit of Selenium scripting (in Java). Currently I'm trying to open a chat box in Facebook and send a message.
I have gotten up to being able to open the chat box through selenium, however I can't figure out how to actually type things into the text box.
Currently here is my code:

我刚刚开始学习一些 Selenium 脚本(在 Java 中)。目前我正在尝试在 Facebook 中打开一个聊天框并发送消息。
我已经能够通过 selenium 打开聊天框,但是我不知道如何在文本框中实际输入内容。
目前这是我的代码:

import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;

public class practice {
    public static void main(String[] args) throws InterruptedException {
      System.setProperty("webdriver.chrome.driver", "C:/max/chromedriver.exe");

      WebDriver driver = new ChromeDriver();
      driver.get("http://facebook.com/login/");
      driver.manage().window().maximize();
      driver.findElement(By.name("email")).sendKeys("[email protected]");
      driver.findElement(By.name("pass")).sendKeys("myPassword");
      driver.findElement(By.id("loginbutton")).click();
      driver.findElement(By.name("mercurymessages")).click();
      driver.findElement(By.cssSelector("a[href*='https://www.facebook.com/messages/conversation-8148306']")).click();
     // This has worked randomly. Sometimes the driver will work and open the chat box. Sometimes it will say element not found. I didn't include the full link of the conversation because apparently you don't have to. And it has worked like this in the past.
}
}

My current issue I'd like resolved is: why does this only work sometimes, and how do i find the text box area on the chat box? I did an Inspect Element and the chat box is very odd. It doesn't have anything like an id or a name, so i can't do By.id, or By.name. I can't figure out how to do it with By.cssSelector. This is what the inspect element for the text box looks like:

我目前想要解决的问题是:为什么这只有时会起作用,我如何在聊天框中找到文本框区域?我做了一个检查元素,聊天框很奇怪。它没有 id 或 name 之类的东西,所以我不能做 By.id 或 By.name。我不知道如何用 By.cssSelector 做到这一点。这是文本框的检查元素的样子:

textarea class="uiTextareaAutogrow _552m" data-ft=".... more stuff here" onkeydown=" more stuff here" style= "more stuff here."

采纳答案by Madhan

You have to learn Xpath and how to create Relative xpath.The xpath for the textarea

您必须学习 Xpath 以及如何创建相对 xpath。textarea 的 xpath

driver.findElement(By.xpath("//textarea[@class='uiTextareaAutogrow _552m']"));

Anyhow I've made few changes that Include instead of clicking on some other message.It will create a new message and send to your friend

无论如何,我做了一些包含而不是点击其他消息的更改。它会创建一个新消息并发送给你的朋友

driver.findElement(By.name("mercurymessages")).click();

    //wait for 20 seconds 
    WebDriverWait wait = new WebDriverWait(driver, 20);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("u_0_5")));
    driver.findElement(By.id("u_0_5")).click();//To click on Send a New Message Link

    //To enter a name into the to field 
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//input[@class='inputtext textInput']")));
    WebElement friendName = driver.findElement(By.xpath("//input[@class='inputtext textInput']"));

    friendName.sendKeys("Deep");//Change it with your friend name

    //wait for the user list to appear
    wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//li[@class='user selected']")));
    friendName.sendKeys(Keys.ENTER);

    WebElement messageBox = driver.findElement(By.xpath("//textarea[@class='uiTextareaAutogrow _552m']"));
    wait.until(ExpectedConditions.visibilityOf(messageBox));
    messageBox.sendKeys("Hi there");

    messageBox.sendKeys(Keys.ENTER);

回答by Jainil Chauhan

How to send message to your friend on Facebook:: Using Selenium WebDriver

如何在 Facebook 上向您的朋友发送消息:: 使用 Selenium WebDriver

// Find Username and Enter
        driver.findElement(By.id("email")).sendKeys("Email");
        System.out.println("Email");

        // Find Password and Enter
        driver.findElement(By.id("pass")).sendKeys("Password");
        System.out.println("Password");

        // Click Login
        driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
        driver.findElement(By.xpath(".//*[@id='u_0_q']")).click();
        System.out.println("LogIn Successfull");
        Thread.sleep(2000);

        // Message
        driver.findElement(By.xpath(".//*[@id='u_0_f']/a/div")).click();
        driver.findElement(By
                .xpath(".//*[@id='u_0_f']/div/div[3]/div/div[1]/div/div/ul/li[2]/a/div/div[2]/div/div[2]/div/div[1]/strong/span"))
                .click();

        Thread.sleep(2000);

        driver.findElement(By.xpath("//*[@class='_1mf _1mj']//following :: div[11]")).click();
        Thread.sleep(2000);
        WebElement sendmsg = driver
                .findElement(By.xpath("//div[@class='_1ia']/descendant::div[@class='_5rpu' and @role='combobox']"));
        sendmsg.sendKeys("Just testing: using selenium webdriver" + Keys.ENTER);

回答by Javier

Hi pal hope this gonna help you:

嗨朋友希望这会帮助你:

there is a method called By.classNameso in order to find elements without an id or a name you can use this method, the thing it's that sometimes the class name of the element it's used in other elements, so be aware of the uses in the current page, if you are lucky and the class name is unique then you can use it :)

有一个方法叫做By.classNameso 为了找到没有 id 或 name 的元素,你可以使用这个方法,问题是有时它在其他元素中使用的元素的类名,所以要注意当前页面中的用途, 如果你很幸运并且类名是唯一的,那么你可以使用它:)

System.setProperty("webdriver.chrome.driver", "C:/max/chromedriver.exe");

      WebDriver driver = new ChromeDriver();
      driver.get("http://facebook.com/login");
      driver.manage().window().maximize();
      Thread.sleep(5000);
      driver.findElement(By.name("email")).sendKeys("[email protected]");
      driver.findElement(By.name("pass")).sendKeys("myPassword");
      driver.findElement(By.id("loginbutton")).click();
      Thread.sleep(5000);
      driver.findElement(By.name("mercurymessages")).click();
      Thread.sleep(7000);// here you have to use ExpectedConditions in order to verify that the elemnt  it′s "clickable"


      driver.findElement(By.cssSelector("a[href*='https://www.facebook.com/messages/conversation-8148306']")).click();

          Thread.sleep(5000);
      WebElement mssgbox = driver.findElement(By.cssSelector("textarea[class*='uiTextareaAutogrow _552m']"));
      mssgbox.click();
        mssgbox.sendKeys("hi");

hope this help you.

希望这对你有帮助。

and some times elements in the pages changes so use ExpectedConditionsto be sure the element its on the page

有时页面中的元素会发生变化,因此请ExpectedConditions确保该元素位于页面上