'Javascript to get link text(s)

I want to get the anchor (here "linktext") of a textlink on a special page:

<div id="msgbox"><h2>xxx</h2><p>xxxx <a href="./url">linktext</a>xxx</p></div>

I tried it with Javascript to get link text and this code:

var e = document.getElementById('msgbox').
        getElementsByTagName('p')[0].
        getElementsByTagName('a')[0];

var theText = e.innerHTML;

console.log(theText);

// or, in sufficiently-modern browsers

e = document.querySelector('#msgbox p a');
theText = e.innerHTML;

return theText ;
}

But i just get something like

"HTMLDivElement: html > body.sliding-popup-processed > div"

as value. There can be more than one linktext on the page, which should be tracked.

Can you help me out?



Solution 1:[1]

If you just want to generate random values, you can use something like this:

id = ''.join(random.choices(string.digits, k=16))

responses.add(
    GET, 
    f'http://app/projects/{id}/tasks?opt_pretty=false', 
    status=200,
    body=json.dumps(res_t)
)

Or if you want to use know values, you can use something like Pytest Parametrize feature. That would look like this:

@pytest.mark.parametrize("id", ["123456789", "897654321", "654789123"])
def test_api(id):

    ...

    responses.add(
        GET, 
        f'http://app/projects/{id}/tasks?opt_pretty=false',
        status=200,
        body=json.dumps(res_t)
    )

With parametrize, your test will run like in a "for loop" which every run, the id will have a different given value.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leonardo Lima