I used xpath for getting the paragraph text, it works fine for first paragraph but second and third paragraph it is not working fine. it shows empty in console output.
Following string are the ones which i have faced the issue - String secondAns , String thirdAns
[Note : I did not face any issue as element not found for second and third paragraph ]
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
CodePudding user response:
As far as I see, second and third paragraphs are hidden (not expanded), so getText()
reruns an empty string for present, but not visible element.
You have to expand(make visible) the elements before getting text.
Try to click on corresponding accordeon headers.
CodePudding user response:
The elements are within an accordion. Within the accordion though the first question is visible by default, the second and the third question is collapsed by default.
So to extract the text you have to click() and open the accordion and you can use the following Locator Strategies:
Code Block:
driver.get("https://demoqa.com/widgets"); 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());
Console Output:
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.