Arindam Roychowdhury
Arindam Roychowdhury

Reputation: 6521

Capture a text from webpage using selenium

I am trying to capture a piece of text from a website which keeps changing. It looks like :

Order ID : XXIO-123344-3456

The prefix is constant but the numbers will always change. I want to capture this number and store it. I have tried storeTextPresent with regex pattern regexp:Email.*@.*com. It does return me a True but it does not return me the value. Of course storeTextPresent is supposed to only return True or False. So now how can I capture the exact value?

Here's a screen shot of the part of the webpage. Can't show the whole page, so sorry.

enter image description here

So any ideas guys?

I export these test after recording into python remote control. So python specific code is more welcome.

Upvotes: 0

Views: 1936

Answers (4)

kreativitea
kreativitea

Reputation: 1791

Python Code.

def get_order_id(driver):
    """ Gets the order id, given an Order Details page. """
    try:
        bFonts = driver.find_element_by_class_name("bFont")
        for element in bFonts:
            if "Order ID" in element.text:
                return element.text.split()[-1]
    except NoSuchElementException:
        return None

This assumes that the class name, bFont never changes. If it does, you can rewrite it to search over the div tag. It also assumes that "Order ID" will be found.

Upvotes: 0

Luke Mills
Luke Mills

Reputation: 1606

Just had a look in the manual. I found the storeText command under Store Commands and Selenium Variables. My guess is that if you use storeText instead of storeTextPresent.

Also, instead of trying to find the text using a regex pattern, you could try using an xpath, DOM, or CSS locator.

Upvotes: 0

Arindam Roychowdhury
Arindam Roychowdhury

Reputation: 6521

Thanks for the idea but i could'nt find a locater for the text. This is the code i captured using firebug .

<div class="chkOutBox">
<h2 id="tnq" class="marb10">Order Details</h2>
<div class="ordRevAddressArea">
<div class="ordRevDelSlotArea">
<div class="clear"></div>
<div class="bFont">Order ID:&nbsp; BBO-72262-171012</div>
<div class="scartPgHdr">
<h3 class="catHdr">Fruits &amp; Vegetables</h3>

Here we are capturing the id number (Line 6)......May be someone can tell me how to figure out a possible locater for this from the above code ....By the way I solved my problem by capturing the URL of the page which had the order ID.I used a regex to seperate out the order id and thats it.......Its only a temporary solution.......

Upvotes: 0

ssebastian
ssebastian

Reputation: 19

assertText command with the regular expression regexp:^XXIO-.+ would resolve the issue. Try this in conjunction with the element id you need to verify.

Upvotes: 1

Related Questions