저는 Selenium을 처음 사용 했으므로 웹 사이트에서 pdf
, ppt(x)
및및파일을 모두 다운로드하고 싶습니다. 다음 코드를 작성했습니다. 하지만 내부 링크를하는 방법에 혼란 스러워요 :Selenium을 사용하여 웹 사이트의 모든 링크를 반복합니다.
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
public class WebScrapper {
String loginPage = "https://blablah/login";
static String userName = "11";
static String password = "11";
static String mainPage = "https://blahblah";
public WebDriver driver = new FirefoxDriver();
ArrayList<String> visitedLinks = new ArrayList<>();
public static void main(String[] args) throws IOException {
System.setProperty("webdriver.gecko.driver", "E:\\geckodriver.exe");
WebScrapper webSrcapper = new WebScrapper();
webSrcapper.openTestSite();
webSrcapper.login(userName, password);
webSrcapper.getText(mainPage);
webSrcapper.saveScreenshot();
webSrcapper.closeBrowser();
}
/**
* Open the test website.
*/
public void openTestSite() {
driver.navigate().to(loginPage);
}
/**
* @param username
* @param Password Logins into the website, by entering provided username and password
*/
public void login(String username, String Password) {
WebElement userName_editbox = driver.findElement(By.id("IDToken1"));
WebElement password_editbox = driver.findElement(By.id("IDToken2"));
WebElement submit_button = driver.findElement(By.name("Login.Submit"));
userName_editbox.sendKeys(username);
password_editbox.sendKeys(Password);
submit_button.click();
}
/**
* grabs the status text and saves that into status.txt file
*
* @throws IOException
*/
public void getText(String website) throws IOException {
driver.navigate().to(website);
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
List<WebElement> allLinks = driver.findElements(By.tagName("a"));
System.out.println("Total no of links Available: " + allLinks.size());
for (int i = 0; i < allLinks.size(); i++) {
String fileAddress = allLinks.get(i).getAttribute("href");
System.out.println(allLinks.get(i).getAttribute("href"));
if (fileAddress.contains("download")) {
driver.get(fileAddress);
} else {
// getText(allLinks.get(i).getAttribute("href"));
}
}
}
/**
* Saves the screenshot
*
* @throws IOException
*/
public void saveScreenshot() throws IOException {
File scrFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(scrFile, new File("screenshot.png"));
}
public void closeBrowser() {
driver.close();
}
}
나는 현재 링크 (단어 "다운로드"를 포함한 ADRESS 포함) 다운로드 할 수있는 파일인지 확인하는 경우 절을합니다. 그렇다면, 나는 그것을 얻을 것이다, 그렇지 않다면, 무엇을해야 할까? 그 부분은 내 문제입니다. 내부 링크를 검색하고 내부 링크에 대한 단계를 반복하는 재귀 함수를 구현하려고했지만 성공하지는 못했습니다.
한편, https://blahblah
을 입력으로 사용할 때 발견되는 첫 번째 링크는 이며 동일한 페이지를 참조하면 https://blahblah
입니다. 그것은 또한 문제를 일으킬 수 있지만, 현재, 나는 또 다른 문제에 갇혀 있고 이것은 재귀의 구현이다. 저 좀 도와 주 시겠어요?
질문을 편집하여 충분한 답변을 식별 할 수있는 세부 정보가있는 특정 문제로 제한하십시오. 한 번에 여러 개의 다른 질문을하지 마십시오. 이 질문에 대한 설명은 [How to Ask] (https://stackoverflow.com/help/how-to-ask) 페이지를 참조하십시오. – DebanjanB
@DebanjanB 죄송합니다. 여러 질문을하지 않았습니다. 내 질문은 간단하고 직관적입니다 : Selenium을 사용하여 웹 사이트의 모든 링크를 반복 할 수 있습니까? 내 게시물에 표시되는 다른 질문을 이해하지 못합니다. 설명해 주시겠습니까? – user1419243
@ user1419243 그냥 'else'문을 제거해보십시오. 'for (int i = 0; i
user1207289