如何使用Selenium验证href URL是否正确形成?

时间:2012-07-11 14:30:30

标签: django xpath selenium

我正在试图弄清楚如何使用Django Selenium Webdriver来测试特定页面上的href URL是否正确形成。

e.g。 http://www.domain.com/post/12/test_post

所以我想测试一个正则表达式:

'^post/(?P<id>\d+)/test_post)$'

虽然我知道帖子的名字(slug),但我不知道ID是什么。 如何使用Selenium测试href URL是否正确形成?

我试过了:

self._driver.find_element_by_xpath("//a[matches(@href='/post/(/?P<id>\d+)/test_post')]")

但是我收到了这个错误:

*** InvalidSelectiorException: Message: u'The given selector //a[matches(@href=\'/post/(/?P<id>\\d+)/test_post\')] is either invalid or does not result in a WebElement. The following error occurred:\n[InvalidSelectorError] Unable to locate an element with the xpath expression //a[matches(@href=\'/post/(/?P<id>\\d+)/test_post\')] because of the following error:\n[Exception... "The expression is not a legal expression."  code: "51" nsresult: "0x805b0033 (NS_ERROR_DOM_INVALID_EXPRESSION_ERR)"  location: "file:///var/folders/m

但我一直在说,“”这个表达不是一个法律表达。“

self._driver.find_element_by_xpath("//a[starts-with(@href, '/post']")  
self._driver.find_element_by_xpath("//a[starts-with(@href, 'post/']")  
self._driver.find_element_by_xpath("//a[starts-with(@href, '/post/']")  

在这里我也得到了“”这个表达不是一个合法的表达。“

有没有办法可以验证href标签是否正确形成?

1 个答案:

答案 0 :(得分:1)

HY,

您似乎没有关闭匹配

方法的括号

尝试:

self._driver.find_element_by_xpath("//a[matches(@href, '/post/(?P<id>\d+)/test_post')]")