为什么SeleniumDriver / Java需要向下滚动才能允许我在Facebook上发布?

问题描述 投票:0回答:1

我曾经/一直试图使用Firefox驱动程序使Selenium Driver在Facebook Post上发布,但通常不能/不能:由于某种原因,我必须使浏览器向下滚动,然后点击“发布”按钮,否则会出现错误。

这是我的代码:

代码清单1:Main.java

package main;

import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

public class Main {

    public static void main(String args[]) {

        System.setProperty("webdriver.gecko.driver", "D:\\PortableApps\\Webdrivers\\geckodriver.exe");

        FirefoxOptions options = new FirefoxOptions()
                  .addPreference("browser.startup.page", 1)
                  .addPreference("permissions.default.desktop-notification", 1)
                  .addPreference("intl.accept_languages", "en-US")
                  .addPreference("browser.startup.homepage", "https://www.google.co.uk");

        WebDriver driver = new FirefoxDriver(options);
        driver.get("https://www.facebook.com");
        driver.findElement(By.className("inputtext")).sendKeys("[email protected]");
        driver.findElements(By.className("inputtext")).get(1).sendKeys("Xrt4LV512");
        driver.findElement(By.className("inputtext")).submit();


        WebDriverWait wait = new WebDriverWait(driver, 60);
        WebElement postBox = wait.until(ExpectedConditions.presenceOfElementLocated(By.id("feedx_sprouts_container")));
        postBox.click();

        postBox = wait.until(ExpectedConditions.presenceOfNestedElementLocatedBy(By.id("feedx_sprouts_container"), By.className("notranslate")));
        postBox.sendKeys("Hello Post!");

        try {
            Thread.sleep(5000);//Necessary because of the way facebook works
        } catch (InterruptedException e) {}

        WebElement postButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#feedx_sprouts_container button[type=submit]")));
        postButton.click();

    }
}

如果我运行该代码,我会从Facebook收到错误提示:

您的请求无法处理

此请求存在问题。我们正在努力尽快修复它。

visual representation of the facebook request couldn't be processed

为了解决这个问题,我不得不使用Selenium and Facebook Post Button: How to click facebook post button in java using selenium?中的解决方案。那就是在Thread.sleep()try / catch之后,我必须使浏览器使用下面的代码向下滚动。

代码清单2:额外

((JavascriptExecutor)driver).executeScript("window.scrollBy(0,259)","");

它有效,但不正常,如果我不知道此错误的原因,恐怕以后我还会与facebook碰壁。

java selenium selenium-webdriver
1个回答
1
投票

尝试下面的代码,使用Actions并更改一些定位符:

WebDriverWait wait = new WebDriverWait(driver, 60);
Actions act = new Actions(driver);

WebElement postBox = wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("xhpc_message")));
act.moveToElement(postBox).click().build().perform();

WebElement postBox2 = wait.until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("._1mf._1mj")));
act.moveToElement(postBox2).click().sendKeys("Hello Post").build().perform();

WebElement postButton = wait.until(ExpectedConditions.elementToBeClickable(By.cssSelector("#feedx_sprouts_container button[type=submit]")));
postButton.click();

正在导入:

import org.openqa.selenium.interactions.Actions;
© www.soinside.com 2019 - 2024. All rights reserved.