Iterating over numbers within XPath
<p>I am using the Selenium webdriver in Python to find elements on a web page to process, in the way shown below: </p>

<pre><code>driver.find_element_by_xpath('//div[@class="gsc-cursor-page" and text()="1"]')
</code></pre>

<p>Ideally, I would like to go through all the texts within that div, <code>text()="1"</code>,  <code>text()="2"</code>, ... <code>text()="10"</code>, etc. But since the whole xpath is a string, somehow I cannot find a way to write an iteration <code>for i in range(10)</code> without breaking the inner structure. </p>

<p>The closest I could get is </p>

<pre><code>mypath = '//div[@class="gsc-cursor-page" and text()="' + str(i) + '"]'
next =  driver.find_element_by_xpath(path)
</code></pre>

<p>But this also leads to error. Any suggestions on how to solve this issue?</p>

<p>This XPath,</p>

<pre><code>//div[@class="gsc-cursor-page" and number() &gt;= 1 and number() &lt;= 10]
</code></pre>

<p>will select all of the <code>div</code> elements of the specified <code>@class</code> with values in the range of <code>1..10</code>.</p>

<hr>

<p><strong>Specific Python/Selenium code to address OP's comment</strong></p>

<pre><code>xp = "//div[@class="gsc-cursor-page" and number() &gt;= 1 and number() &lt;= 10]"

for div in driver.find_elements_by_xpath(xp):
    div.click()
</code></pre>

python html xml selenium xpath