gpt4 book ai didi

java - 使用 Selenium 和 Java 在 Facebook 中发布状态

转载 作者:行者123 更新时间:2023-12-02 03:53:15 26 4
gpt4 key购买 nike

如何使用 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();
}

}

最佳答案

  1. 据我所知,您的 XPath 表达式不太正确 title相关textarea看起来像:

    What's on your mind, user1984

    因此您需要修改定位器以使用 XPath contains() function像:

    By.xpath("//textarea[contains(@title,\"What's on your mind\")]")
  2. 使用Thread.sleepperformance 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");

关于java - 使用 Selenium 和 Java 在 Facebook 中发布状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56765823/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com