2

I am automating one Project in my organization and came across one issue. I am very new to automation and selenium.

Issue that i am facing: not able to capture the text inside the dd tag in the html

enter image description here

The Highlighted text in the UI is what I want to capture and adjacent to it is the html code

Also Attaching the script I am writing And the Console Outputenter image description here

enter image description here

I am able to fetch the value from the dt tag but not from dd tag

Can Somebody help me with this?

7
  • First of all - please post code and not images. There is no way someone can quote the code in their answers if you post images. If you are referring to the dd following the dt shouldn't the XPaths look the same except for dt changing to dd? Commented Sep 25, 2018 at 5:07
  • Demo = (driver.findElement(By.xpath(("//div[@class='row p-3']/div/div/div/dl/dt[1]"))).getText()); System.out.println(Demo); driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); Kogan_Name = driver.findElement(By.xpath("\"//div[@class='row p-3']/div/div/div/dl/dd[1]")).getText(); System.out.println(Kogan_Name); I replaced the dt with dd and upon validating the xpath on console it seems to work fine but when i am using the same xpath in script it says invalid selector error Commented Sep 25, 2018 at 5:17
  • I replaced the dt with dd but still it did not capture the text. I Also tried .getattribute("innerText") and .getattribute("TextContent") but nothing is working out Commented Sep 25, 2018 at 6:30
  • Looking at implicitWait call before the getText() - I guess you expect the data to be present after some time. ImplicitWait waits only for the element to be present. Can you change it to a sleep() and check whether getText() returns proper value? Commented Sep 25, 2018 at 6:45
  • No it did not work with sleep and get text Commented Sep 25, 2018 at 6:55

4 Answers 4

1

This below code will surely help you.

Try this

public void click(WebDriver driver) throws InterruptedException {
            WebDriverWait wait = new WebDriverWait(driver, 30);
        try {
            Thread.sleep(3000);
            WebElement table = driver.findElement(By.className("ng-star-inserted"));
            List<WebElement> allrows = table.findElements(By.tagName("tr"));
          //  List<WebElement> allcols = table.findElements(By.tagName("td"));
            Thread.sleep(2000);
            for(WebElement row: allrows) {
                List<WebElement> Cells = row.findElements(By.tagName("td"));
                for(WebElement Cell:Cells) {
                    if (Cell.getText().contains("COASTAL"))
                        Cell.click();
                }
            }
            Thread.sleep(2000);
            System.out.println("Welcome");
            Thread.sleep(10000);
            wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("call-caseDetail"))).click();
            System.out.println("first row is clicked");
            createDirector(wait, driver);
            updateDirector(wait, driver);
            deleteDirector(driver);
        }
        catch (Exception e) {
            System.out.println("Timeout " + e);
        } 
1

Not sure if you got your answer already.

If you didn't, then a few possibilities would be -

koganName = driver.findElement(By.xpath(("//div[@class='row p-3']/div/div/div/dl/dt[1]/following-sibling::dd[1]"))).getText();

koganName = driver.findElement(By.xpath(("//div[@class='row mb-1']//dd[1]")).getText();

koganName = driver.findElement(By.xpath(("//div[@class='row mb-1']//dt[text() = 'Name:']/following-sibling::dd[1]")).getText();

Although, in each case, the XPath is still long and you can optimize it most definitely.

Also, like @Dakshinamurthy Karra mentioned, it would be more helpful if you posted the code snippet instead of screen captures.

Good luck and happy testing!

10
  • Hi Stephen, I tried your suggestions but nothing seems to work out :( Also tried .getattribute("innerText") and .getattribute("TextContent") with the xpath but still nothing is going inside the Kogan_Name variable Commented Sep 25, 2018 at 7:36
  • Did you try it out like this: WebElement element= driver.findElement(By.xpath(""//div[@class='row p-3']//dl[@class='row mb-1']/dd[1]")); String name = element.getAttribute("innerText"); What happens when you give the absolute xpath: driver.findElement(By.xpath("//html/body/app-root/app-track/div/div/div[2]/div[2]/div/div/div/dl/dd[1]")).getText();
    – Stephen
    Commented Sep 25, 2018 at 7:54
  • none is working Stephen. Commented Sep 25, 2018 at 8:11
  • Hmm.. do you have firefox with firepath installed? Perhaps you can capture the xpath it provides and try checking if it works as is in your code? Without the DOM to work with, I am taking shots in the dark myself. Also, the implictlyWait will only wait for the element to be available and not the property/value itself. So yes, adding a thread.sleep() might also mitigate the issue.
    – Stephen
    Commented Sep 25, 2018 at 8:17
  • Tried thread.sleep() as well. Let me try to install firepath and check the xpath once Commented Sep 25, 2018 at 8:19
1

Have you tried using CSS instead of XPath? Also, your XPath expression seems too long. You may need to shorten it. I would try something like //div[@class='row p-3']. CSS expression for the same is div[class='row p-3'].

1
  • it is working with that xpath the issue is it is not capturing the text inside the dd tag Commented Sep 25, 2018 at 6:36
0

This is a bit horizontal, but you might consider looking at using Protractor (wrapper of Selenium) to test this web app that looks like it must be Angular, before you get too far. The value(s) you seek to extract might be more easily available via a Protractor specific locator (like ngmodel).

2
  • Hello.. Yes it is an angular application.. But I am already using Cucumber - Selenium framework is it possible to use Protector with Cucumber? Commented Sep 26, 2018 at 4:07
  • Perhaps with CucumberJS Commented Sep 26, 2018 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.