Home > Net >  Getting wrong text value when using Xpath locator
Getting wrong text value when using Xpath locator

Time:11-13

I'm currently learning Selenium and was given an test case to create, using store.steampowered.com/search/?filter=topsellers as an test site. I need to get number that is near checkbox "Action", as highlighted on a screenshot.

This is a part of a page code that describes this specific checkbox:

<div class="tab_filter_control_row " data-param="tags" data-value="19" data-loc="Action" data-clientside="0">
    <span class="tab_filter_control tab_filter_control_include " data-param="tags" data-value="19" data-loc="Action" data-clientside="0" data-gpfocus="item">
        <span>
                <span class="tab_filter_control_checkbox"></span>
                <span class="tab_filter_control_label">Action</span>
                <span class="tab_filter_control_count" style="">30</span>
        </span>
    </span>
                <span class="tab_filter_control_not " data-param="untags" data-value="19" data-icon="https://store.akamai.steamstatic.com/public/images/search_crouton_not.svg" data-loc="Action" data-clientside="0" data-tooltip-text="Exclude results with this tag" data-gpfocus="item"><img src="https://store.akamai.steamstatic.com/public/images/search_checkbox_not.svg" width="16px" height="16px"></span>
        </div>

To do that I wrote this XPath locator: //span[@data-value='19']//*[@class='tab_filter_control_count']. But when I do something like this:

string str = driver.FindElement(By.XPath("//span[@data-value='19']//*[@class='tab_filter_control_count']"));

I for some reason will get "1 635", not "30". I tried to get text by using different XPath, //div[@data-collapse-name='tags']//*[@data-value='19'], but result will be "Action 1 635".

Maybe somebody will explain how 30 turns into 1 635 and how I can get the right number.

CodePudding user response:

Its my dumb mistake, I didn'n put a wait between previous actions and a moment when I tried to grab this value, so I recieved something random from partially loaded page.

CodePudding user response:

To extract the text 30 next to the label Action you can use the following Locator Strategy:

  • Using xpath and Text attribute:

    Console.WriteLine(driver.FindElement(By.XPath("//span[text()='Action']//following::span[1]")).Text);
    

Ideally, you have to induce WebDriverWait for the desired ElementIsVisible and you can use the following Locator Strategy:

  • Using xpath and GetAttribute():

    Console.WriteLine(new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementIsVisible(By.XPath("//span[text()='Action']//following::span[1]"))).GetAttribute("innerHTML"));
    
  • Related