java - 为什么 Selenium webDriver/Java 需要向下滚动才能允许我在 facebook 上发帖?

标签 java selenium selenium-webdriver

我曾经/正在尝试让 Selenium 驱动程序通常使用 Firefox 驱动程序发布 Facebook 帖子,但无法/无法正常执行:出于某种原因,我必须在单击之前让浏览器向下滚动“发布”按钮,否则我会收到错误。

这是我的代码:

代码 list 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("ghostbrain@hotmail.fr");
        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 收到一条错误消息,指出:

Your request couldn't be processed

There was a problem with this request. We're working on getting it fixed as soon as we can.

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 之后使用以下代码使浏览器向下滚动。

代码 list 2:额外

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

它可以工作,但它不正常,如果我不知道这个错误背后的原因,我担心我以后在 facebook 上会碰上另一堵墙。

最佳答案

尝试下面的代码,使用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;

关于java - 为什么 Selenium webDriver/Java 需要向下滚动才能允许我在 facebook 上发帖?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58495089/

相关文章:

java - Jmeter进行QPS测试好像不精确?

java - 如何处理在新选项卡中自动打开的新窗口?

java - 元素MyElement在点(x,y)不可点击...其他元素将获得点击

java - 在 Java 中处理日期和时间的不一致

java - 如何使用JWebBrowser访问DOM模型?

java - 浏览器不显示 java applet

python - 使用 Selenium 在 Web 自动化过程中加载动画时出现 PyQt5 卡住问题

ios - 在应用程序中自动化 iOS webkit

python - 从 Python Selenium 中的 USD 中提取 AUD 的价格 - Web Scraping

java - 如何解决通过selenium访问网页元素而不滚动页面的问题