Автоматизируйте график SVG с помощью Selenium JavaJAVA

Программисты JAVA общаются здесь
Ответить
Anonymous
 Автоматизируйте график SVG с помощью Selenium Java

Сообщение Anonymous »

Ссылка на веб-сайт: https://www.worldometers.info/world-pop ... opulation/
На самом деле я хочу получить значения, когда навожу курсор на строку графика и собирать данные за каждый год. Годы - с 1951 по 2024 год, так как это сделать
Я пытаюсь использовать координатор x и y svg rect, использовать класс действий селена и использовать moveToELement(), и он не работает, я не знаю почему
Мой код
package selenium_concepts.SVGElements;
import org.openqa.selenium.*;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Action;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import java.time.Duration;
import java.util.List;
public class IndianPopulationSVGElementTest {
WebDriver driver;
JavascriptExecutor jse;
@BeforeTest
public void setup() {
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(20));
driver.get("https://www.worldometers.info/world-pop ... opulation/");
}
@Test(description = "India Population Graph data test")
public void svgGraphIndiaPopulation() throws InterruptedException {
jse = ((JavascriptExecutor) driver);
WebElement popGraph = driver.findElement(By.xpath("//*[local-name()='svg']//*[name()='g']"));
jse.executeScript("arguments[0].scrollIntoView(true);", popGraph);
String heading = driver.findElement(By.cssSelector("#maincounter-wrap h1")).getText();
String totalPopulationIndia = driver.findElement(By.cssSelector(".maincounter-number span")).getText();
String indianPopulationBetweenYear = driver.findElement(By.xpath("(//div[@class='chart-header'])[1]")).getText();
Thread.sleep(5000);
System.out.println(" -------------- " + heading + " ----------------- ");
System.out.println("Total Population India: " + totalPopulationIndia);
System.out.println("Between Years: " + indianPopulationBetweenYear);
WebElement rectPop = driver.findElement(By.xpath("((//*[local-name()='svg'])[1]//*[name()='g']//*[name()='rect'])[1]"));
Dimension dimension = rectPop.getSize();
int rectHeight = dimension.getHeight();
int rectWidth = dimension.getWidth();
int X = rectPop.getLocation().getX();
int Y = rectPop.getLocation().getY();
System.out.println("X: " + X);
System.out.println("Y: " + Y);
System.out.println("Dimension: " + dimension);
System.out.println("Height: " + rectHeight);
System.out.println("Width: " + rectWidth);
// height == 260
// y = (260/2) - 260 = -130
int getTopLeftY = ((rectPop.getSize().getHeight()) / 2 - rectPop.getSize().getHeight());
// weight = 520
// X = (520/2) - 520 == -260
int getTopLeftX = ((rectPop.getSize().getWidth()) / 2 - rectPop.getSize().getWidth());
Actions actions = new Actions(driver);
for(int i = 0; i < 50; i++) {
actions.moveToElement(rectPop, getTopLeftX, getTopLeftY).perform();
}
/*

WebElement popGraphSVG = driver.findElement(By.xpath("//*[local-name()='svg']//*[name()='defs' and @id='_ABSTRACT_RENDERER_ID_0']"));
Actions actions = new Actions(driver);
actions.moveToElement(popGraphSVG).perform();
Thread.sleep(4000);
actions.click();

List tooltipsContents = driver.findElements(By.xpath("//*[local-name()='svg']//*[name()='g' and @class='google-visualization-tooltip']//*[name()='g']//*[name()='text']"));
String populationYear = tooltipsContents.getFirst().getText();
String totalPopulationInYear = tooltipsContents.get(2).getText();

System.out.println(populationYear + " ----> " + totalPopulationInYear);

WebElement tracker = driver.findElement(By.xpath("(//*[local-name()='svg']//*[name()='g']//*[name()='circle'])[1]"));

for (int i = 1; i

Подробнее здесь: https://stackoverflow.com/questions/788 ... enium-java
Ответить

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

Вернуться в «JAVA»