无法使用 Selenium Java 提取手风琴中的段落文本

Unable to extract the paragraph text within an accordion using Selenium Java

我使用 xpath 获取段落文本,第一段工作正常,但第二和第三段工作不正常。它在控制台输出中显示为空。

以下字符串是我遇到过的问题 - 字符串 secondAns、字符串 thirdAns

[注意:我没有遇到任何问题,因为找不到第二段和第三段的元素]

driver.get("https://demoqa.com/widgets");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//span[text()='Accordian']")).click(); 
//clicking the left menu 
//First Paragraph
String firstques = driver.findElement(By.xpath("//div[@id='section1Heading']")).getText();
System.out.println("First Question : " + firstques); 
String firstAns = driver.findElement(By.xpath("//div[@id='section1Content']//p[1]")).getText(); 
System.out.println("First Answer : " + firstAns); 
    
//Second Paragraph
String secondques = driver.findElement(By.xpath("//div[@id='section2Heading']")).getText();
System.out.println("Second Question : " + secondques); 
String secondAns = driver.findElement(By.xpath("//div[@id='section2Content']//p[1]")).getText(); 
System.out.println("Second Answer : " + secondAns); 
    
//Third Paragraph
String thirdques = driver.findElement(By.xpath("//div[@id='section3Heading']")).getText();
System.out.println("Third Question : " + thirdques); 
String thirdAns = driver.findElement(By.xpath("//div[@id='section3Content']//p[1]")).getText(); 
System.out.println("Third Answer : " + thirdAns

据我所知,第二和第三段是隐藏的(未展开),因此 getText() 重新运行一个空字符串,但不显示元素。

您必须在获取文本之前展开(使其可见)元素。

尝试点击相应的手风琴headers。

这些元素在 手风琴 中。在 accordion 中,虽然第一个问题默认是 visible,但第二个和第三个问题默认是折叠的。


所以要提取文本,您必须 and open the and you can use the following :

  • 代码块:

    driver.get("https://demoqa.com/widgets");
    ((JavascriptExecutor)driver).executeScript("scroll(0, 100)");
    new WebDriverWait(driver, 20).until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@id='section2Heading']"))).click();
    System.out.println(new WebDriverWait(driver, 20).until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//div[@id='section2Heading']//following::div[1]/div[@id='section2Content']"))).getText());
    
  • 控制台输出:

    Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.
    The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from "de Finibus Bonorum et Malorum" by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham.