java - Selenium instagram 追随者抛出过时的元素参考
问题描述
我正在为机器学习收集数据,并且正在努力抓取其中一个社交网络页面以获取数据。我一直很好,直到第 12 个追随者,然后它崩溃了。
方法 driver.findElement(By.xpath... 停止在第 13 个元素上工作,即使它正在滚动。谁能指出它不可见的原因?这是我到现在为止的想法:
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
public class Testing {
public static ChromeOptions options;
public static WebDriver driver;
public static void setup() {
System.setProperty("webdriver.chrome.driver", "resources/Drivers/chromedriver.exe");
options = new ChromeOptions();
driver = new ChromeDriver(options);
driver.get("https://www.instagram.com/accounts/login/");
}
public static void login() throws Exception {
String name = "xxxxxx";
String password = "xxxxxxx";
Thread.sleep(2000);
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.tagName("form")).click();
WebDriverWait wait = new WebDriverWait(driver, 15);
wait.until(ExpectedConditions.presenceOfElementLocated(By.linkText("See All")));
driver.get(String.format("https://www.instagram.com/officialrickastley/"));
driver.findElement(By.partialLinkText("follower")).click();
try {
Thread.sleep(3000);
for (int j = 1; j < 1000; j += 11) {
System.out.println("j " + j);
WebElement element = driver.findElement(By.xpath(("/html/body/div[4]/div/div[2]/ul/div/li[" + (j) + "]/div/div[2]/div[1]/div/div/a")));
Actions actions = new Actions(driver);
actions.moveToElement(element);
actions.perform();
for (int i = 1; i < 12; i++) {
System.out.println("user: " + i + " " + driver.findElement(By.xpath(("/html/body/div[4]/div/div[2]/ul/div/li[" + (i + j) + "]/div/div[2]/div[1]/div/div/a")))
.getAttribute("title"));
Thread.sleep(300);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
driver.close();
}
}
}
解决方案
请尝试添加显式等待以等待元素可见,然后执行所需的操作。
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("ur xpath here")));
推荐阅读
- android - 没有互联网连接时向 FirebaseDatabase 添加数据
- android - 在 startActivityForResult 中传递额外数据在 onActivityResult 中从另一个类接收到 null
- android - 通知在 android api 28 上无法正常工作
- c++ - OpenGL UV 的行为很奇怪?
- swift - 根据最近关于用于在 VC 之间传递数据的闭包的问题,在 rootVC 中使用 containerVC 时我将如何做同样的事情?
- python - 如何在一个文件中分别运行多个python方法
- go - 反映运行时错误:在零值上调用 reflect.flag.mustBeAssignable
- .net-core - Swagger 授权承载不发送
- python - 加减一个二进制数后得到不同的值
- google-sheets - 谷歌电子表格查询;保留背景颜色