Instagram不会通过Selenium拍我的照片
1。项目:试图使用Android Studio
- 系统中的Selenium和Java进行重新发布应用程序:MacOS 11.6.6
- 驱动程序:Chromedriver 102.0.5005.61
- 编程语言:Java
- IDE:Java IDE:Android Studio Chipmunk | 2021.2.1补丁1
2。代码:
`公共类MainActivity扩展了AppCompatactivity {
public static <i> void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Users/johannes/Downloads/chromedriver-2");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");
new WebDriverWait(driver, Duration.ofSeconds(10));
driver.findElement(By.className("HoLwm")).click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.name("username")));
driver.findElement(By.name("username")).sendKeys("//yourusername");
driver.findElement(By.name("password")).sendKeys("//yourpassword");
// Variablen Einfügen ->
new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = driver.findElement(By.cssSelector("button[type='submit']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
String url = ("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");
String currentURL = driver.getCurrentUrl();
while (url == currentURL) {
WebElement elementz = driver.findElement(By.cssSelector("button[type='submit']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementz);
}
new WebDriverWait(driver, Duration.ofSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement Nichtjetzt = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Not Now']")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
WebDriverWait waitt = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement NichtjetzT = waitt.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Not Now']")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
NichtjetzT.click();
new WebDriverWait(driver, Duration.ofSeconds(10));
WebDriverWait waits = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement Nichtjetztz = waits.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Jetzt nicht']")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
Nichtjetztz.click();
// Variable Einfügen ->
String Suche = "designatgarten";
driver.findElement(By.xpath("//input[@placeholder='Suchen']")).sendKeys(Suche);
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.className("-qQT3")));
driver.findElement(By.className("-qQT3")).click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#react-root > section > main > div > div.fx7hk > a._9VEo1.T-jvg > span > span")));
url = "https://www.instagram.com";
driver.getCurrentUrl();
if (!currentURL.equals(url)) {
JavascriptExecutor js = (JavascriptExecutor) driver;
try {
long lastHeight = ((Number) js.executeScript("return document.body.scrollHeight")).longValue();
while (true) {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
Thread.sleep(2000);
long newHeight = ((Number) js.executeScript("return document.body.scrollHeight")).longValue();
if (newHeight == lastHeight) {
break;
}
lastHeight = newHeight;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
List<WebElement> srcList = driver.findElements(By.tagName("img"));
srcList.stream().filter(sa -> sa.getAttribute("src").startsWith("cdninstagram.com"))
.collect(Collectors.toList());
WebElement removedStr = srcList.remove(0);
WebElement removedStr2 = srcList.remove(srcList.size()-1);
WebElement removedStr3 = srcList.remove(srcList.size()-1);
System.out.printf("Found %s links to images\n", srcList.size());
for (WebElement sa : srcList) {
System.out.println("[" + sa.getAttribute("src") + "]");
//Lösung entfernen von Highlights
}
List<String> strings = new ArrayList<String>();
for(WebElement e : srcList){
strings.add(e.getAttribute("src"));
}
int n = 0;
String LetztesFoto;
String LetztesElement = strings.get(strings.size()-1 + n);
LetztesFoto = LetztesElement;
try(InputStream in = new URL(LetztesFoto).openStream()){
Files.copy(in, Paths.get("/Users/johannes/AndroidStudioProjects/InstagramRepost/app/src/main/java/com/example/image.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
n -= 1;
driver.findElement(By.cssSelector("#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg.KtFt3 > div > div:nth-child(3) > div > button > div > svg")).click();
WebElement currentElement = driver.findElement(By.cssSelector("input[type=file]"));
currentElement.sendKeys("/Users/johannes/AndroidStudioProjects/InstagramRepost/app/src/main/java/com/example/image.jpg");
}
}`
3。问题:如果我在Android Studio上运行代码,则没有显示出失败或其他任何错误,但 Instagram似乎已经得到了照片,或者至少有一些东西,因为在代码之后完成后,它重新加载了页面,并将我带回主页“
4。有问题的部分:在我看来,这部分是代码:
driver.findElement(By.cssSelector("#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg.KtFt3 > div > div:nth-child(3) > div > button > div > svg")).click();
WebElement currentElement = driver.findElement(By.cssSelector("input[type=file]"));
currentElement.sendKeys("/Users/johannes/AndroidStudioProjects/InstagramRepost/app/src/main/java/com/example/image.jpg");
在某个地方引起问题。我认为这可能是由绝对路径引起的以上
。
-
input [@accept ='image/jpeg']
它们俩在Android Studio中造成了错误中引起了错误
1. Project: Trying to make a repost app with Selenium and Java in Android Studio
- System: MacOS 11.6.6
- Driver: ChromeDriver 102.0.5005.61
- Programming Language: Java
- IDE: Android Studio Chipmunk | 2021.2.1 Patch 1
2. Code:
`public class MainActivity extends AppCompatActivity{
public static <i> void main(String[] args) throws InterruptedException {
System.setProperty("webdriver.chrome.driver", "/Users/johannes/Downloads/chromedriver-2");
ChromeDriver driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");
new WebDriverWait(driver, Duration.ofSeconds(10));
driver.findElement(By.className("HoLwm")).click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.name("username")));
driver.findElement(By.name("username")).sendKeys("//yourusername");
driver.findElement(By.name("password")).sendKeys("//yourpassword");
// Variablen Einfügen ->
new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement element = driver.findElement(By.cssSelector("button[type='submit']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", element);
String url = ("https://www.instagram.com/accounts/login/?hl=en&source=auth_switcher");
String currentURL = driver.getCurrentUrl();
while (url == currentURL) {
WebElement elementz = driver.findElement(By.cssSelector("button[type='submit']"));
((JavascriptExecutor) driver).executeScript("arguments[0].click();", elementz);
}
new WebDriverWait(driver, Duration.ofSeconds(10));
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement Nichtjetzt = wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Not Now']")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
WebDriverWait waitt = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement NichtjetzT = waitt.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Not Now']")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
NichtjetzT.click();
new WebDriverWait(driver, Duration.ofSeconds(10));
WebDriverWait waits = new WebDriverWait(driver, Duration.ofSeconds(10));
WebElement Nichtjetztz = waits.until(ExpectedConditions.elementToBeClickable(By.xpath("//button[text()='Jetzt nicht']")));
new WebDriverWait(driver, Duration.ofSeconds(10)).until(
webDriver -> ((JavascriptExecutor) webDriver).executeScript("return document.readyState").equals("complete"));
Nichtjetztz.click();
// Variable Einfügen ->
String Suche = "designatgarten";
driver.findElement(By.xpath("//input[@placeholder='Suchen']")).sendKeys(Suche);
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.elementToBeClickable(By.className("-qQT3")));
driver.findElement(By.className("-qQT3")).click();
new WebDriverWait(driver, Duration.ofSeconds(10)).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("#react-root > section > main > div > div.fx7hk > a._9VEo1.T-jvg > span > span")));
url = "https://www.instagram.com";
driver.getCurrentUrl();
if (!currentURL.equals(url)) {
JavascriptExecutor js = (JavascriptExecutor) driver;
try {
long lastHeight = ((Number) js.executeScript("return document.body.scrollHeight")).longValue();
while (true) {
((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight);");
Thread.sleep(2000);
long newHeight = ((Number) js.executeScript("return document.body.scrollHeight")).longValue();
if (newHeight == lastHeight) {
break;
}
lastHeight = newHeight;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
List<WebElement> srcList = driver.findElements(By.tagName("img"));
srcList.stream().filter(sa -> sa.getAttribute("src").startsWith("cdninstagram.com"))
.collect(Collectors.toList());
WebElement removedStr = srcList.remove(0);
WebElement removedStr2 = srcList.remove(srcList.size()-1);
WebElement removedStr3 = srcList.remove(srcList.size()-1);
System.out.printf("Found %s links to images\n", srcList.size());
for (WebElement sa : srcList) {
System.out.println("[" + sa.getAttribute("src") + "]");
//Lösung entfernen von Highlights
}
List<String> strings = new ArrayList<String>();
for(WebElement e : srcList){
strings.add(e.getAttribute("src"));
}
int n = 0;
String LetztesFoto;
String LetztesElement = strings.get(strings.size()-1 + n);
LetztesFoto = LetztesElement;
try(InputStream in = new URL(LetztesFoto).openStream()){
Files.copy(in, Paths.get("/Users/johannes/AndroidStudioProjects/InstagramRepost/app/src/main/java/com/example/image.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
n -= 1;
driver.findElement(By.cssSelector("#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg.KtFt3 > div > div:nth-child(3) > div > button > div > svg")).click();
WebElement currentElement = driver.findElement(By.cssSelector("input[type=file]"));
currentElement.sendKeys("/Users/johannes/AndroidStudioProjects/InstagramRepost/app/src/main/java/com/example/image.jpg");
}
}`
3. Problem: If I run the code on Android Studio it shows no errors that it has failed or anything but Instagram seems to have got the Photo or at least it has got something because after the code is finished it reloads the page and brings me back to the mainpage"
4. Problematic Part: In my opinion this part of the code:
driver.findElement(By.cssSelector("#react-root > section > nav > div._8MQSO.Cx7Bp > div > div > div.ctQZg.KtFt3 > div > div:nth-child(3) > div > button > div > svg")).click();
WebElement currentElement = driver.findElement(By.cssSelector("input[type=file]"));
currentElement.sendKeys("/Users/johannes/AndroidStudioProjects/InstagramRepost/app/src/main/java/com/example/image.jpg");
Causes the problem somewhere. I think it is likely caused by the Absolute Path above.
5. Already tried Approaches :
input[@type='file']
input[@accept = 'image/jpeg']
Both of them caused an error in Android StudioChanging the Download Location from a folder on my desktop to the folder where the main activity is
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论