0

I have earlier used jmeter css selector and XPath extractor post processor to retrieve Csrf token. Is there anyway to use these in locust as well

I want to fetch from value attribute

nikhil bisht
  • 21
  • 1
  • 3

2 Answers2

0

You can use LXML FOR XPath

from lxml import html
...
    @task
    my_task(self):
        response = self.client.post(...)
        tree = html.fromstring(response.text)
        # <div title="buyer-name">Carson Busses</div>
        # <span class="item-price">$29.95</span> 
        buyers = tree.xpath('//div[@title="buyer-name"]/text()')

(example from https://docs.python-guide.org/scenarios/scrape/)

Another option is to use pythons built in HTML parser, but that is probably more complicated for this use case (https://docs.python.org/3/library/html.parser.html)

You could also use a regular expression for a quick and dirty solution:

import re
message_regex = re.compile(r"...") # use a regex that actually matches your desired text
...
    @task
    my_task(self):
        response = self.client.post(...)
        m = message_regex.match(response.text)
        my_value = m.group(1)
Cyberwiz
  • 11,027
  • 3
  • 20
  • 40
  • as per your solution, it will fetch text, but I want to fetch value attribute – nikhil bisht Jul 19 '20 at 08:06
  • I'm not that good at lxml, but I think you should be able to figure it out by reading this: https://lxml.de/tutorial.html#using-xpath-to-find-text – Cyberwiz Jul 20 '20 at 14:28
0

I have used pyquery to fetch value attribute using css selector and xpath

First we need to import pyquery

from pyquery import PyQuery as pq

for css selector

response = self.client.get("/edtwrkkd?id=251612")
           tree=pq(response.text)
           Test=tree("#formTest")
           self.Token=Test("input[name='__RequestVerificationToken']").val()

for xpath

Test =tree("form[method='post']")
            self.Token=Test("input[name='__RequestVerificationToken']").val()
        
nikhil bisht
  • 21
  • 1
  • 3