Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Selenium和Facebook帖子按钮:如何使用Selenium在java中单击Facebook帖子按钮?_Java_Facebook_Selenium - Fatal编程技术网

Selenium和Facebook帖子按钮:如何使用Selenium在java中单击Facebook帖子按钮?

Selenium和Facebook帖子按钮:如何使用Selenium在java中单击Facebook帖子按钮?,java,facebook,selenium,Java,Facebook,Selenium,代码的最后一行不起作用。如何在facebook上设置Post按钮的XPath?我知道您已经标记了自己的答案,但这不是正确的方法。Selenium中内置了一些方法来进行等待,例如等待某个元素可单击。这种方法是正确且更有效的方法 driver.get("https://www.facebook.com/sachin.aryal"); driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium

代码的最后一行不起作用。如何在facebook上设置Post按钮的XPath?

我知道您已经标记了自己的答案,但这不是正确的方法。Selenium中内置了一些方法来进行等待,例如等待某个元素可单击。这种方法是正确且更有效的方法

driver.get("https://www.facebook.com/sachin.aryal");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
driver.findElement(By.xpath("//*[@id='u_0_1a']/div/div[6]/div/ul/li[2]/button")).click();

我注意到,当您第一次滚动到“查看”按钮时,它会起作用,请在单击“发布”之前尝试添加:

driver.get("https://www.facebook.com/sachin-aryal/");
driver.findElement(By.name("xhpc_message_text")).sendKeys("Testing Java and Selenium");
WebDriverWait wait = new WebDriverWait(driver, 10);
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button/span[.=\"Post\"]"))).click();

请尝试下面的代码。。它一定对你有用

     ((JavascriptExecutor)driver).executeScript("window.scrollBy(0,259)","");
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;



public class Facebook_login {

    public static void main(String[] args) throws InterruptedException {
        String user_name = "facebook_user_name";
        String pwd = "facebook_password";
        WebDriver driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get("https://www.facebook.com");
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
        driver.findElement(By.name("email")).clear();
        driver.findElement(By.name("email")).sendKeys(user_name);
        driver.findElement(By.name("pass")).clear();
        driver.findElement(By.name("pass")).sendKeys(pwd);
        driver.findElement(By.xpath("//input[contains(@value,'Log In')]")).click();
        Thread.sleep(10000);                
        System.out.println("logged in successfully");
        WebElement notification = driver.findElement(By.xpath("//a[contains(@action,'cancel')]"));
        if(notification.isDisplayed()) {
            System.out.println("Notification is present");
            notification.click();
        }
        WebElement status =driver.findElement(By.xpath("//textarea[@name='xhpc_message']"));
        status.sendKeys("Hello");
        Thread.sleep(3000);
        WebDriverWait wait = new WebDriverWait(driver,30);
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//span[text()='Post']"))).click();


    }
}