0

New to Protractor scripting.

Any feedback on why I am unable to find element in the stack trace based on this following run?

TempString = browser.driver.findElement(by.xpath("/html/body/section/div/form/section/section[2]/div/div/div[1]/div/div/div[3]/div[2]/div[2]/div[1]/div[1]/h3")).getText();

This is "monthly payment" field at the mortgagecalculator.org site.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
ColtsFan
  • 13
  • 4
  • I tried both single quotes and the double around the string; no luck. – ColtsFan Mar 17 '15 at 23:52
  • Have you tried: `//*@id="calc"]/form/section/section[2]/div/div/div[1]/div/div/div[3]/div[2]/div[2]/div[1]/div[1]/h3`? This is what Chrome's element inspector gave me. In any case, I think you need a double forward slash at the beginning. – Isaac Lyman Mar 17 '15 at 23:58
  • @IsaacLyman I think this is an overcomplication to have an xpath like this, check out my answer. Thanks. – alecxe Mar 18 '15 at 01:19
  • That's a good way of doing it. I would like it noted that I personally never use xpath because it's so fragile--the slightest change of the page wipes out all your xpaths. I much prefer cleverly-made CSS paths. But your xpath should be pretty solid, since it finds wording that isn't likely to change. – Isaac Lyman Mar 18 '15 at 18:00
  • @alecxe slow learning of protractor js syntax: having trouble finding good source of documentation for "List" methods. ie. my script errors trying to traverse the array.....any thought? see answer – ColtsFan Mar 26 '15 at 11:22
  • @alecxe I recently used a List object of type in a selenium junit test but I am having trouble with js in protractor script ie. my script below runs fine through the findElements line but I cannot traverse the array. ps. non-angular web site thus browser.driver ref. – ColtsFan Mar 26 '15 at 11:58
  • @ColtsFan please go through your questions and accept the answers if they deserve to be accepted (checkbox on left to the answer). – alecxe Mar 29 '15 at 17:07

2 Answers2

2

Make it simple.

Rely on the "Monthly payment" text and get the preceding-sibling:

//span[contains(., "Monthly Payment")]/preceding-sibling::h3

Since there are two elements with "Monthly Payment" text, you can use element.all() to get both and use first() and last() to get the desired one:

var monthlyPayments = element.all(by.xpath('//span[contains(., "Monthly Payment")]/preceding-sibling::h3'))

monthlyPayments.first().getText();  
monthlyPayments.last().getText();
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0
browser.driver.findElement(by.xpath("//input[@value='Calculate']")).click();
        var TempStringList;
        var fTempString;
        var sTempString;

    //attempt to get everything on the page with span label Monthly     Payment - note there are two
    TempStringList = browser.driver.findElements(by.xpath('//span    [contains(., " Monthly Payment")]/preceding-sibling::h3'));
    //GOOD TO HERE ON RUN BUT PROBLEM BELOW WITH NAVIGATING THE     ARRAY LIST
    fTempString = TempStringList.first().getText();
ColtsFan
  • 13
  • 4