我刚刚开始学习一些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("myemail@yahoo.com");
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.
}
}
我当前要解决的问题是:为什么这只会有效,我如何在聊天框中找到文本框区域?我做了一个Inspect Element,聊天框很奇怪。它没有像id或name这样的东西,所以我不能做By.id或By.name。我无法弄清楚如何使用By.cssSelector来做到这一点。这就是文本框的inspect元素:
textarea class="uiTextareaAutogrow _552m" data-ft=".... more stuff here" onkeydown=" more stuff here" style= "more stuff here."
答案 0 :(得分:1)
你必须学习Xpath以及如何创建相对xpath。textarea的xpath
driver.findElement(By.xpath("//textarea[@class='uiTextareaAutogrow _552m']"));
无论如何,我做了一些改动,包括而不是点击其他消息。它会创建一条新消息并发送给你的朋友
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);
答案 1 :(得分:0)
嗨,朋友希望这会帮助你:
有一个名为By.className
的方法,所以为了找到没有id或名称的元素,你可以使用这个方法,它有时候是元素的类名,它是'用于其他元素,所以要注意当前页面中的用法,如果你很幸运,并且类名是唯一的,那么你可以使用它:)
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("myemail@yahoo.com");
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");
希望这对你有所帮助。
并且页面中的某些元素会发生变化,因此请使用ExpectedConditions
确保页面上的元素
答案 2 :(得分:0)
如何在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);