如何使用Selenium和Java在Facebook中发布状态?我尝试了下面的代码,该代码不起作用。可以登录,但在发布状态时却没有错误。登录后,我得到通知弹出窗口允许或阻止,如何还要处理这个弹出窗口?在我用于测试的代码下面。
public class NewTest {
private WebDriver driver;
@Test
public void testEasy() throws InterruptedException {
driver.get("https://www.facebook.com/");
Thread.sleep(5000);
driver.findElement(By.id("email")).sendKeys("email");
driver.findElement(By.id("pass")).sendKeys("password" + Keys.ENTER);
Thread.sleep(5000);
driver.findElement(By.xpath("//textarea[@title=\"What's on your mind?\"]")).click();
driver.findElement(By.xpath("//textarea[@title=\"What's on your mind?\"]")).sendKeys("Hello World");
driver.findElement(By.xpath("//textarea[@title=\"What's on your mind?\"]")).sendKeys(Keys.ENTER);
}
@BeforeTest
public void beforeTest() {
System.setProperty("webdriver.chrome.driver",
"C:\\Users\\admin\\Desktop\\Test\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
}
@AfterTest
public void afterTest() {
driver.quit();
}
}
答案 0 :(得分:0)
据我所见,相关title中的textarea看起来很不正确,您的XPath表达式如下:
What's on your mind, user1984
因此,您需要修改定位符以使用XPath contains()
function,例如:
By.xpath("//textarea[contains(@title,\"What's on your mind\")]")
使用Thread.sleep是performance anti-pattern,应该改用WebDriverWait。重构代码示例:
driver.get("https://www.facebook.com/");
WebDriverWait wait = new WebDriverWait(driver, 5);
wait.until(ExpectedConditions.elementToBeClickable(By.id("email"))).sendKeys("email");
wait.until(ExpectedConditions.elementToBeClickable(By.id("pass"))).sendKeys("password" + Keys.ENTER);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(@title,\"What's on your mind\")]"))).click();
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//textarea[contains(@title,\"What's on your mind\")]"))).sendKeys("Hello World");