我使用 xpath 來獲取段落文本,它適用于第一段,但第二段和第三段無法正常作業。它在控制臺輸出中顯示為空。
以下字串是我遇到的問題 - String secondAns , StringthirdAns
[注意:我沒有遇到任何問題,因為第二段和第三段沒有找到元素]
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
uj5u.com熱心網友回復:
元素在手風琴內。在手風琴中,雖然默認情況下第一個問題是可見的,但默認情況下第二和第三個問題是折疊的。

因此,要提取文本,您必須單擊()并打開手風琴,您可以使用以下定位器策略:
代碼塊:
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.
uj5u.com熱心網友回復:
據我所知,第二段和第三段是隱藏的(未展開),因此getText()重新運行一個空字串以顯示當前但不可見的元素。
在獲取文本之前,您必須擴展(使元素可見)。
嘗試單擊相應的手風琴標題。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/391987.html
