How to select a span element using selenium in python?
<p>I am trying to select month which is a table element using the below code where I am finding the table element using <code>xpath</code>. </p>

<p>This code works:</p>

<pre><code>month = driver.find_element_by_xpath('/html/body/div[11]/div[3]/table/tbody/tr/td/span[text()="Aug"])
month.click()
</code></pre>

<p>Here, instead of <code>span[8]</code>, I am trying to store the text in a variable and trying to run the below code:</p>

<pre><code>year = str(raw_input("Enter year")
month = driver.find_element_by_xpath('/html/body/div[11]/div[3]/table/tbody/tr/td/span[text()= year])
month.click()
</code></pre>

<p>But this code is not working.</p>

<p>Actually your question is not much clear, I think you want to append <code>year</code> variable value into your <code>xpath</code> to locate an element with input text, to achieve this you should try as below :-</p>

<pre><code>year = str(raw_input("Enter year")
month = driver.find_element_by_xpath("/html/body/div[11]/div[3]/table/tbody/tr/td/span[text() = '" + year + "']")
month.click()
</code></pre>

<p>You can summarise above <code>xpath</code> with text as well which would also work as :-</p>

<pre><code>month = driver.find_element_by_xpath(".//span[text() = '" + year + "']")
month.click()
</code></pre>

python html python-2.7 selenium-webdriver html-table