Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何匹配a中的当前日期<;td></td>;基于datetime.date.today()?_Python_String_Selenium_Selenium Webdriver - Fatal编程技术网

Python 如何匹配a中的当前日期<;td></td>;基于datetime.date.today()?

Python 如何匹配a中的当前日期<;td></td>;基于datetime.date.today()?,python,string,selenium,selenium-webdriver,Python,String,Selenium,Selenium Webdriver,各位!!我在匹配两个字符串时遇到了一些问题 因此,我从正在测试的页面中获得了以下HTML代码: <td class="fixedColumn ng-binding" ng-style="{'padding':'5px','line-height':'10px'}" style="padding: 5px; line-height: 10px;"> (today's date: 2016-09-23) </td> 显然,当我运行脚本时,浏览器会关闭,根据driver

各位!!我在匹配两个字符串时遇到了一些问题

因此,我从正在测试的页面中获得了以下HTML代码:

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

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

</td>
显然,当我运行脚本时,浏览器会关闭,根据
driver.close()
,但我需要shell打印“一切正常!”在shell中,浏览器通过“单击”转到“主页”

我对Python真的很陌生,但就我而言,我已经尽了一切可能让它工作。谁能给我一些提示并指出我遗漏了什么?谢谢:)

currentDate周围的引号使XPath引用包含实际文本“currentDate”的内容,而不是currentDate变量引用的文本,您需要将其更改为:

todayDate = driver.find_element_by_xpath("//td[contains(text(), " + currentDate + "]")
您可能还需要将currentDate包装在str(currentDate)中,以确保将其转换为字符串,这是我曾经遇到的问题

python中的“+”将字符串连接在一起,因此它应该可以查找变量所引用的文本。希望这能帮你解决问题

Jon Clements建议的另一种不使用python变量的方法:


您可以使用字符串格式来更清楚地说明这一点,例如:

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

只需确保前面有一个from datetime导入日期…

好的,自从我添加了一条评论以来已经有10分钟了,但我已经成功了。 前代码:

todayDate = driver.find_element_by_xpath("//td[contains(text(), 
" + str(currentDate) + ")]")
以下代码:

todayDate = driver.find_element_by_xpath("//td[contains(text(), 
'" + str(currentDate) + "')]")
我把
“+str(currentDate)+”
放在
'
中。还有,我变了

`if todayDate == currentDate:` to `if todayDate:`

现在它可以正常工作了,我只需要找出原因:)无论如何,感谢AntlerFox和Jon Clements给您带来的麻烦。

您的currentDate被引号包围,因此它将查找实际包含currentDate的内容,而不是current date变量引用的文本,请取出这些内容并尝试。编辑:我认为还有更多的内容,实际上,让我写一个答案。你可以使用字符串格式来更清楚地说明这一点,例如:
“//td[contains(text(),{})]”。格式(date.today())
-只要确保有一个
from datetime import date
,这也是一个好方法,我会把它添加到答案中,并给你一点信任。谢谢你们!请看下面的评论。我把
“+str(currentDate)+”
放在
之间,它现在可以工作了。如果todayDate==currentDate,我也将
更改为
如果todayDate
:)
todayDate = driver.find_element_by_xpath("//td[contains(text(), 
'" + str(currentDate) + "')]")
`if todayDate == currentDate:` to `if todayDate:`