0

How to Handle Dynamic Changing IDs inside a span In XPath? for example :

  <span id="listv2_f40a0e90dbcc089cae10d48a489619ee_total_rows" style="margin-left: 1px;">2000</span>

In this xpath id value "f40a0e90dbcc089cae10d48a489619ee" is changing every time when reloading the page.

Is there any way to handle this type of situation in Selenium? Below is the outer HMTL , where I need to extract the text 2000 present in total_rows

<div class="vcr_controls">
    <input aria-label="Skip to row" id="listv2_f40a0e90dbcc089cae10d48a489619ee_first_row" aria-describedby="f40a0e90dbcc089cae10d48a489619ee_description_of_skip_to" class="list_row_number_input form-control  " autocomplete="off" title="" style="color:rgb(72, 85, 99); display:inline; height:21px" disabled="" data-original-title="Skip to row">
        <span class="sr-only" id="f40a0e90dbcc089cae10d48a489619ee_description_of_skip_to">Showing rows 1 to 1 of 1</span>
        <span style="margin-left: 5px;" class=" list_row_number_input ">
                    to
            <span id="listv2_f40a0e90dbcc089cae10d48a489619ee_last_row" style="margin-left: 1px; margin-right: 1px">1</span>
                    of
            <span id="listv2_f40a0e90dbcc089cae10d48a489619ee_total_rows" style="margin-left: 1px;">2000</span>
        </span>
    </div>

2 Answers 2

1

Assuming "*total_rows" is unique and constant - I think this should work to get to the span:

"//span[contains(@id, 'total_rows')]"

4
  • org.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //span[contains(@id,'total_rows')] (tried for 20 second(s) with 500 milliseconds interval) Commented Sep 19, 2019 at 10:47
  • 1
    Verify that the xpath is ok first - go to the web page with the element, open console and do (in chrome): $x("//span[contains(@id, 'total_rows')]") If this works it's not a locator issue - see if the element is hidden behind something in the test or try increasing wait time for the element to load Commented Sep 25, 2019 at 8:56
  • yes i was able to locate the XPath in console, $x("//span[contains(@id, 'total_rows')]") (4) [span#listv2_98a4a6fe1b88c49011ef337cdc4bcb44_total_rows, span#98a4a6fe1b88c49011ef337cdc4bcb44_rep_total_rows, span#listv2_94a4a6fe1b88c49011ef337cdc4bcb45_total_rows, span#94a4a6fe1b88c49011ef337cdc4bcb45_rep_total_rows],tried increasing the time out to 40 seconds and even 60 seconds but still no luck Commented Sep 26, 2019 at 12:02
  • 1
    Ah, so the "total_rows" is not unique. Try pointing to the correct one with an index - "(//span[contains(@id, 'total_rows')])[2]" If the correct one remains and is still not clickable, your problem is not the selector anymore. tho It's not a really good solution - as soon as something will be added this will probably break. Commented Oct 4, 2019 at 18:17
0

Try to find the unique string from attribute value and use xpath

//span[contains(@attribute, 'unique string')]

1
  • code WebDriverWait wait = new WebDriverWait(driver, 20); wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//span[contains(@id,'total_rows')]"))); String ele = driver.findElement(By.xpath("//span[contains(@id,'total_rows')]")).getText(); System.out.println(ele); boldorg.openqa.selenium.TimeoutException: Expected condition failed: waiting for visibility of element located by By.xpath: //span[contains(@id,'total_rows')] (tried for 20 second(s) with 500 milliseconds interval) Commented Sep 19, 2019 at 10:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.