1
こんにちは、私はJavaでSelenium WebDriverに問題があります。私はこのコードを使用する場合(element.click();
を使用せずに、それが動作します)セレンでテキストを取得できません
public static void main(String[] args) {
try{
File salida= new File("salidas/Salida.txt");
FileWriter fw = new FileWriter(salida);
PrintWriter volcado = new PrintWriter(fw);
System.setProperty("webdriver.chrome.driver", "path to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://ranking-empresas.eleconomista.es/REPSOL-PETROLEO.html");
String name = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[1]/td[2]")).getText();
String obj_soc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[2]/td[2]")).getText();
String direcc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[3]/td[2]")).getText();
String loc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[4]/td[2]")).getText();
String tel = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[5]/td[2]")).getText();
String url = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[8]/td[2]")).getText();
String actividad = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[9]/td[2]")).getText();
volcado.print(name+" "+obj_soc+" "+direcc+" "+loc+" "+tel+" "+url+" "+actividad);
volcado.close();
driver.close();
}catch(Exception e){
e.printStackTrace();
}}
しかし、私はこのようなelement.click();
と、前のページでアクセスするときに問題がcames:
System.setProperty("webdriver.chrome.driver", "path to\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("http://ranking-empresas.eleconomista.es/ranking_empresas_nacional.html");
WebElement element = driver.findElement(By.xpath("//*[@id=\"tabla-ranking\"]/table/tbody/tr[1]/td[7]/a"));
element.click();
String name = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[1]/td[2]")).getText();
String obj_soc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[2]/td[2]")).getText();
String direcc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[3]/td[2]")).getText();
String loc = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[4]/td[2]")).getText();
String tel = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[5]/td[2]")).getText();
String url = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[8]/td[2]")).getText();
String actividad = driver.findElement(By.xpath("//*[@id=\"business-profile\"]/div[17]/div[1]/div[2]/table/tbody/tr[9]/td[2]")).getText();
volcado.print(name+" "+obj_soc+" "+direcc+" "+loc+" "+tel+" "+url+" "+actividad);
volcado.close();
driver.close();
}catch(Exception e){
e.printStackTrace();
}}
セレンは、ブラウザを開いて、そしてページは私の変数はxpathのテキストを取得しません。