How can I match the current date in a <td></td> based on datetime.date.today()?
<p>everyone! I'm having some trouble in matching two strings.</p>

<p>So, I've got this HTML code from a page I'm testing:</p>

<pre><code>&lt;td class="fixedColumn ng-binding" 
ng-style="{'padding':'5px','line-height':'10px'}" style="padding: 5px; line-height: 10px;"&gt;

(today's date: 2016-09-23)

&lt;/td&gt;
</code></pre>

<p>The actual string that's displayed in the page is <strong>(today's date: 2016-09-23)</strong>.</p>

<p>What I tried to do using Python is this:</p>

<pre><code>#check if today's date = current date
currentDate = datetime.date.today().strftime("(today's date: %Y-%m-%d)")
todayDate = driver.find_element_by_xpath("//td[contains(text(), 'currentDate']")
allOk = 'all good!'
notOk = 'still not OK...'
if todayDate == currentDate:
    print(allOk)
    home = driver.find_element_by_xpath("//a[@title='Home']").click()
else:
    print(notOk)
    driver.close()
</code></pre>

<p>What happens when I run the script is, obviously, that the browser closes, according to <code>driver.close()</code> , but I need the shell to print 'all good!' in the shell and for the browser to go to "Home" by "clicking".</p>

<p>I'm really new to Python, but as far as I'm concerned, I did everything possible to make this work. Could anyone give me some hints and point out to what I'm missing? Thank you :)</p>

<pre><code>todayDate = driver.find_element_by_xpath("//td[contains(text(), 'currentDate']")
</code></pre>

<p>The quotes around currentDate are making the XPath refer to something that contains the actual text 'currentDate', not the text that the currentDate variable refers to, you need to change it to this:</p>

<pre><code>todayDate = driver.find_element_by_xpath("//td[contains(text(), " + currentDate + "]")
</code></pre>

<p>you may also need to wrap currentDate in str(currentDate) to make sure it casts as a string, that was a problem I once faced.</p>

<p>'+' in python concatenates strings together, so this should make it look for the text that variable refers to. Hope this fixes it for you!</p>

<p>Another method that does not use a python variable as suggested by Jon Clements:</p>

<p>You can make this clearer by using string formatting instead, eg:</p>

<pre><code>"//td[contains(text(), {})]".format(date.today())
</code></pre>

<p>just make sure there's a from datetime import date before hand...</p>

python string selenium selenium-webdriver