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
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
I am able to fetch the value from the dt tag but not from dd tag
Can Somebody help me with this?
4 Answers 4
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);
}
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!
-
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 variableshreya_bhatngr– shreya_bhatngr2018年09月25日 07:36:23 +00:00Commented 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– Stephen2018年09月25日 07:54:06 +00:00Commented Sep 25, 2018 at 7:54
-
none is working Stephen.shreya_bhatngr– shreya_bhatngr2018年09月25日 08:11:16 +00:00Commented 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– Stephen2018年09月25日 08:17:35 +00:00Commented Sep 25, 2018 at 8:17
-
Tried thread.sleep() as well. Let me try to install firepath and check the xpath onceshreya_bhatngr– shreya_bhatngr2018年09月25日 08:19:43 +00:00Commented Sep 25, 2018 at 8:19
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']
.
-
it is working with that xpath the issue is it is not capturing the text inside the dd tagshreya_bhatngr– shreya_bhatngr2018年09月25日 06:36:14 +00:00Commented Sep 25, 2018 at 6:36
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).
-
Hello.. Yes it is an angular application.. But I am already using Cucumber - Selenium framework is it possible to use Protector with Cucumber?shreya_bhatngr– shreya_bhatngr2018年09月26日 04:07:33 +00:00Commented Sep 26, 2018 at 4:07
-
Perhaps with CucumberJSSubjective Reality– Subjective Reality2018年09月26日 17:19:45 +00:00Commented Sep 26, 2018 at 17:19
Explore related questions
See similar questions with these tags.
dd
following thedt
shouldn't the XPaths look the same except fordt
changing todd
?implicitWait
call before thegetText()
- 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 asleep()
and check whethergetText()
returns proper value?