PlanHandle.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. package com.lala.yj.controller;
  2. import lombok.SneakyThrows;
  3. import org.openqa.selenium.By;
  4. import org.openqa.selenium.JavascriptExecutor;
  5. import org.openqa.selenium.WebDriver;
  6. import org.openqa.selenium.chrome.ChromeDriver;
  7. import org.openqa.selenium.chrome.ChromeOptions;
  8. import org.openqa.selenium.remote.DesiredCapabilities;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11. import java.util.Set;
  12. import java.util.concurrent.TimeUnit;
  13. /**
  14. * @author yj
  15. * @date 2020/12/18 下午2:08
  16. */
  17. public class PlanHandle implements Runnable {
  18. public PlanHandle(List<PlanDto> dto) {
  19. this.dto = dto;
  20. }
  21. private List<PlanDto> dto;
  22. // public void changeWindow(WebDriver driver) {
  23. // // 存储原始窗口的 ID
  24. // String originalWindow = driver.getWindowHandle();
  25. //
  26. //// 检查一下,我们还没有打开其他的窗口
  27. // assert driver.getWindowHandles().size() == 1;
  28. //
  29. //// 点击在新窗口中打开的链接
  30. // driver.findElement(By.linkText("new window")).click();
  31. //
  32. //// 等待新窗口或标签页
  33. // wait.until(numberOfWindowsToBe(2));
  34. //
  35. //// 循环执行,直到找到一个新的窗口句柄
  36. // for (String windowHandle : driver.getWindowHandles()) {
  37. // if (!originalWindow.contentEquals(windowHandle)) {
  38. // driver.switchTo().window(windowHandle);
  39. // break;
  40. // }
  41. // }
  42. //
  43. //// 等待新标签完成加载内容
  44. // wait.until(titleIs("Selenium documentation"));
  45. // }
  46. public static String getLastHandle(WebDriver driver) {
  47. //获取当前打开窗口的所有句柄
  48. Set<String> Allhandles = driver.getWindowHandles();
  49. ArrayList<String> lst = new ArrayList<String>(Allhandles);
  50. return lst.get(lst.size()-1);
  51. }
  52. @SneakyThrows
  53. @Override
  54. public void run() {
  55. ChromeOptions options = new ChromeOptions();
  56. DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
  57. desiredCapabilities.setJavascriptEnabled(true);
  58. options.setExperimentalOption("debuggerAddress", "127.0.0.1:9222");
  59. WebDriver driver = new ChromeDriver(options);
  60. for (PlanDto d : dto) {
  61. if ("//*[@id=\"wxadcontainer\"]/div[1]/div[2]/div[2]/div[4]/div[1]/div/div[2]/button".equals(d.getXpath())) {
  62. // 存储原始窗口的 ID
  63. driver.findElement(By.xpath(d.getXpath())).click();
  64. TimeUnit.SECONDS.sleep(1);
  65. String newHandle = getLastHandle(driver);
  66. // 循环执行,直到找到一个新的窗口句柄
  67. driver.switchTo().window(newHandle);
  68. continue;
  69. }
  70. if (d.getType() == 1) {
  71. if ("//*[@id=\"target_next_step\"]".equals(d.getXpath())) {
  72. driver.findElement(By.xpath(d.getXpath())).click();
  73. System.out.println("123123123123");
  74. driver.findElement(By.xpath(d.getXpath())).click();
  75. Set<String> windowHandles = driver.getWindowHandles();
  76. windowHandles.forEach(System.out::println);
  77. try {
  78. TimeUnit.SECONDS.sleep(2);
  79. } catch (InterruptedException e) {
  80. e.printStackTrace();
  81. }
  82. } else if ("//*[@id=\"test_material_container\"]/div[1]/div".equals(d.getXpath())) {
  83. try {
  84. TimeUnit.SECONDS.sleep(1);
  85. } catch (InterruptedException e) {
  86. e.printStackTrace();
  87. }
  88. driver.findElement(By.xpath(d.getXpath())).click();
  89. } else if ("//*[@id=\"choose_template\"]".equals(d.getXpath())) {
  90. try {
  91. TimeUnit.SECONDS.sleep(2);
  92. } catch (InterruptedException e) {
  93. e.printStackTrace();
  94. }
  95. driver.findElement(By.xpath(d.getXpath())).click();
  96. } else if ("/html/body/div[7]/div/div/div[2]/div/div[1]/div[2]/div/div/div[2]/div[2]/button[1]".equals(d.getXpath())) {
  97. try {
  98. TimeUnit.SECONDS.sleep(2);
  99. } catch (InterruptedException e) {
  100. e.printStackTrace();
  101. }
  102. driver.findElement(By.xpath(d.getXpath())).click();
  103. } else if ("//*[@id=\"test_creative_next_step\"]".equals(d.getXpath())) {
  104. try {
  105. TimeUnit.SECONDS.sleep(2);
  106. } catch (InterruptedException e) {
  107. e.printStackTrace();
  108. }
  109. driver.findElement(By.xpath(d.getXpath())).click();
  110. } else if ("//*[@id=\"test_creative_next_step\"]".equals(d.getXpath())) {
  111. driver.findElement(By.xpath(d.getXpath())).click();
  112. try {
  113. TimeUnit.SECONDS.sleep(3);
  114. } catch (InterruptedException e) {
  115. e.printStackTrace();
  116. }
  117. ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
  118. } else if ("//*[@id=\"test_confirmed_protocol\"]".equals(d.getXpath())) {
  119. System.out.println(111);
  120. try {
  121. TimeUnit.SECONDS.sleep(3);
  122. } catch (InterruptedException e) {
  123. e.printStackTrace();
  124. }
  125. ((JavascriptExecutor) driver).executeScript("window.scrollTo(0, document.body.scrollHeight)");
  126. driver.findElement(By.xpath(d.getXpath())).click();
  127. // driver.findElement(By.xpath(d.getXpath())).click();
  128. } else {
  129. driver.findElement(By.xpath(d.getXpath())).click();
  130. }
  131. /**
  132. *
  133. js1="document.documentElement.scrollTop=10000"
  134. driver.execute_script(js1)
  135. */
  136. } else {
  137. driver.findElement(By.xpath(d.getXpath())).sendKeys(d.getKeys());
  138. }
  139. }
  140. }
  141. }