为什么在使用 selenium webdriver 读取正文中提到的站点上显示的距离时没有返回任何内容
why nothing is returned when reading distance displayed on the site mentioned in the body using selenium webdriver
我正在尝试从站点 (https://www.freemaptools.com/distance-between-canada-postcodes.htm) 的 'Distance by Land Transport' 字段中读取距离值,但是尽管计算出的距离值显示在该字段中,但我什么也没得到。
下面是我的代码和使用的网站。
问题:
- 为什么我一无所获? (请看下面的代码字符串变量:strDist 什么都不显示)
- 有趣的是,当我检查元素时,值显示为 0.00。请看下面的屏幕截图。
我好像没看懂
@Test
public void TestDist() {
System.setProperty("webdriver.chrome.driver", "C:\Users\...\eclipse-workspace\...\...\src\main\java\...\...\drivers\chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch Website
driver.navigate().to("https://www.freemaptools.com/distance-between-canada-postcodes.htm");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scrollBy(0, 500)");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[1]/input")).sendKeys("N3S 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[3]/input")).sendKeys("N3r 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[4]/p")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String strDist = driver.findElement(By.xpath("//*[@id=\"tb_transport\"]")).getText();
System.out.println(strDist);
}
enter image description here
Can you try with the below code, I hope it will help you
@Test
public void TestDist() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\Users\...\eclipse-workspace\...\...\src\main\java\...\...\drivers\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Launch Website
driver.navigate().to("https://www.freemaptools.com/distance-between-canada-postcodes.htm");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scrollBy(0, 500)");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[1]/input")).sendKeys("N3S 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[3]/input")).sendKeys("N3r 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[4]/p")).click();
Thread.sleep(5000);
WebElement element2 = driver.findElement(By.xpath("//*.
[@id='tb_transport']"));
String text = (String) js.executeScript("return
document.getElementById('tb_transport').value", element2);
System.out.println(text);
I have used the JavaScriptExecutor to get the value from the textbox
which is readyonly
我正在尝试从站点 (https://www.freemaptools.com/distance-between-canada-postcodes.htm) 的 'Distance by Land Transport' 字段中读取距离值,但是尽管计算出的距离值显示在该字段中,但我什么也没得到。 下面是我的代码和使用的网站。
问题:
- 为什么我一无所获? (请看下面的代码字符串变量:strDist 什么都不显示)
- 有趣的是,当我检查元素时,值显示为 0.00。请看下面的屏幕截图。
我好像没看懂
@Test
public void TestDist() {
System.setProperty("webdriver.chrome.driver", "C:\Users\...\eclipse-workspace\...\...\src\main\java\...\...\drivers\chromedriver.exe");
WebDriver driver=new ChromeDriver();
// Launch Website
driver.navigate().to("https://www.freemaptools.com/distance-between-canada-postcodes.htm");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor)driver;
js.executeScript("scrollBy(0, 500)");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[1]/input")).sendKeys("N3S 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[3]/input")).sendKeys("N3r 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[4]/p")).click();
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String strDist = driver.findElement(By.xpath("//*[@id=\"tb_transport\"]")).getText();
System.out.println(strDist);
}
enter image description here
Can you try with the below code, I hope it will help you
@Test
public void TestDist() throws InterruptedException {
System.setProperty("webdriver.chrome.driver",
"C:\Users\...\eclipse-workspace\...\...\src\main\java\...\...\drivers\chromedriver.exe");
WebDriver driver = new ChromeDriver();
// Launch Website
driver.navigate().to("https://www.freemaptools.com/distance-between-canada-postcodes.htm");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("scrollBy(0, 500)");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[1]/input")).sendKeys("N3S 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[3]/input")).sendKeys("N3r 7X6");
driver.findElement(By.xpath("//*[@id=\"content\"]/form/table/tbody/tr[2]/td[4]/p")).click();
Thread.sleep(5000);
WebElement element2 = driver.findElement(By.xpath("//*.
[@id='tb_transport']"));
String text = (String) js.executeScript("return
document.getElementById('tb_transport').value", element2);
System.out.println(text);
I have used the JavaScriptExecutor to get the value from the textbox
which is readyonly