我曾经/正在尝试让 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.
为了解决这个问题,我必须使用 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/