So for this exercise we will be clicking an any state in this SVG map.
https://petdiseasealerts.org/forecast-map#/
So for this exercise, the best chance you getting a valid result is getting the correct xpath. So most of you will get this xpath.
String stateXpath =
"(//*[name()='svg'])/*[@id='features']/*[@id='regions']/*[@id='"+state+"']";
or this
String stateXpath= "//*[name()='svg']//*[name()='g' and @id = ' "+state+" ']";
with one of this implementations if you ran following code, you will fail at state Florida, with element click intercept exception.
package org.example;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* Hello world!
*
*/
public class App
{
public static WebDriver driver;
public static void main( String[] args ) throws InterruptedException {
String state ="Georgia";
String stateXpath =
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* Hello world!
*
*/
public class App
{
public static WebDriver driver;
public static void main( String[] args ) throws InterruptedException {
String state ="Georgia";
String stateXpath =
"(//*[name()='svg'])/*[@id='features']/*[@id='regions']/*[@id='"+state+"']";
By framename = By.xpath("//iframe[contains(@id,'map-instance')]");
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions);
driver.get("https://petdiseasealerts.org/forecast-map#/");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
Thread.sleep(10000);
//Implementation 1
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(framename));
driver.findElement(By.xpath(stateXpath)).click();
}
}
By framename = By.xpath("//iframe[contains(@id,'map-instance')]");
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions);
driver.get("https://petdiseasealerts.org/forecast-map#/");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
Thread.sleep(10000);
//Implementation 1
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(framename));
driver.findElement(By.xpath(stateXpath)).click();
}
}
But if you get the correct xpath there won't be any issue in automating any state in here. Also need to keep in mind that we have to use js.executeScript to execute the click, normal click will not work for this scenario.
package org.example;
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* Hello world!
*
*/
public class App
{
public static WebDriver driver;
public static void main( String[] args ) throws InterruptedException {
String state ="Georgia";
By framename = By.xpath("//iframe[contains(@id,'map-instance')]");
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions);
driver.get("https://petdiseasealerts.org/forecast-map#/");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
Thread.sleep(10000);
//Implementation 2
String commonXpath = "//*[@id='regions']/*[@class='region']";
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(framename));
List<WebElement> statesList = driver.findElements(By.xpath(commonXpath));
System.out.println("States count: "+ statesList.size());
String newXpath = commonXpath +
import io.github.bonigarcia.wdm.WebDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import java.util.List;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
/**
* Hello world!
*
*/
public class App
{
public static WebDriver driver;
public static void main( String[] args ) throws InterruptedException {
String state ="Georgia";
By framename = By.xpath("//iframe[contains(@id,'map-instance')]");
ChromeOptions chromeOptions = new ChromeOptions();
WebDriverManager.chromedriver().setup();
driver = new ChromeDriver(chromeOptions);
driver.get("https://petdiseasealerts.org/forecast-map#/");
driver.manage().window().maximize();
JavascriptExecutor js = (JavascriptExecutor) driver;
js.executeScript("javascript:window.scrollBy(250,350)");
Thread.sleep(10000);
//Implementation 2
String commonXpath = "//*[@id='regions']/*[@class='region']";
WebDriverWait wait = new WebDriverWait(driver,30);
wait.until(ExpectedConditions.frameToBeAvailableAndSwitchToIt(framename));
List<WebElement> statesList = driver.findElements(By.xpath(commonXpath));
System.out.println("States count: "+ statesList.size());
String newXpath = commonXpath +
"/*[@class='rpath']/*[contains(@name,'"+state+"')]";
js.executeScript("arguments[0].dispatchEvent(new Event('click'))",
js.executeScript("arguments[0].dispatchEvent(new Event('click'))",
driver.findElement(By.xpath(newXpath)));
}
}
}
}
Thats the best solution for this scenario.
Comments
Post a Comment