英文:
Javascript executor unable to scroll due to activities on page (Selenium automation)
问题
以下是我的脚本,测试了使用Javascript执行器的各种情况。然而,在最后一步上失败了,即滚动操作。我注意到在导航到页面后,元素加载的时间异常长。我尝试了几种等待选项,但都没有成功。
package UI;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class JavascriptExecutor1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver","C:\\browserdrivers\\chromedriver.exe");
ChromeDriverManager.getInstance().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
//driver.manage().window().maximize();
// 接受弹窗
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"accept-choices\"]"))).click();
JavascriptExecutor jxect = (JavascriptExecutor) driver; // 类似于javascript命令库的API
// 这将打印页面的标题。
String script = "return document.title;"; // 脚本是显示标题的字符串
String title = (String) jxect.executeScript(script); // 告诉javascript执行脚本并将其保存在标题变量中
System.out.println(title); // 打印出我们执行的任何脚本,保存在标题变量中。
// 我们想要点击按钮
driver.switchTo().frame("iframeResult");
jxect.executeScript("myFunction()");
//Thread.sleep(2000);
driver.switchTo().alert().accept();
// 高亮显示按钮
WebElement button = driver.findElement(By.xpath("/html/body/button"));
jxect.executeScript("arguments[0].style.border='5px solid red'", button);
// 滚动
driver.navigate().to("https://www.w3schools.com/");
WebElement getstarted = driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[17]/div/a"));
jxect.executeScript("arguments[0].scrollIntoView(true);", getstarted);
}
}
英文:
Below is my script testing various scenarios with Javascript executor. However, it fails on the last step, which was to scroll. I noticed that after navigating to the page, the elements were taking an exceptionally long time to load. I tried several wait options but didn't work.
package UI;
import java.time.Duration;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import io.github.bonigarcia.wdm.ChromeDriverManager;
public class JavascriptExecutor1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
//System.setProperty("webdriver.chrome.driver","C:\\browserdrivers\\chromedriver.exe");
ChromeDriverManager.getInstance().setup();
ChromeDriver driver = new ChromeDriver();
driver.get("https://www.w3schools.com/jsref/tryit.asp?filename=tryjsref_alert");
//driver.manage().window().maximize();
// accept popup
WebDriverWait wait = new WebDriverWait(driver,Duration.ofSeconds(30));
wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//*[@id=\"accept-choices\"]"))).click();
JavascriptExecutor jxect = (JavascriptExecutor) driver; // Like API to javascript command library
// This will print the title of the page.
String script = "return document.title;"; // The script is title display as string
String title = (String) jxect.executeScript(script); // Tells javascript to execute the script n save it in title variable
System.out.println(title); //Printing out whatever script we we executed that is saved in title variable.
// We want to click the button
driver.switchTo().frame("iframeResult");
jxect.executeScript("myFunction()");
//Thread.sleep(2000);
driver.switchTo().alert().accept();
//To highlight a button
WebElement button = driver.findElement(By.xpath("/html/body/button"));
jxect.executeScript("arguments[0].style.border='5px solid red'", button);
//to scroll
driver.navigate().to("https://www.w3schools.com/");
WebElement getstarted = driver.findElement(By.xpath("/html/body/div[4]/div/div[1]/div[17]/div/a"));
jxect.executeScript("argument[0].scrollintoview(true);", getstarted);
}
}
答案1
得分: 1
你的 scrollintoview
方法中有拼写错误 - 应该是 scrollIntoView
。
以及 argument
变量中也有拼写错误 - 应该是 arguments
。
以下是已翻译的代码部分:
driver.get("https://www.w3schools.com/");
WebElement getStarted = driver.findElement(By.cssSelector("#getdiploma"));
jxect.executeScript("arguments[0].scrollIntoView(true);", getStarted);
英文:
You have typo in method scrollintoview
- it should be scrollIntoView
.
And typo in argument
variable - it should be arguments
Code below works for me:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
driver.get("https://www.w3schools.com/");
WebElement getStarted = driver.findElement(By.cssSelector("#getdiploma"));
jxect.executeScript("arguments[0].scrollIntoView(true);", getStarted);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论