2012-07-11 35 views
0

我想弄清楚如何使用Django Selenium Webdriver來測試特定頁面上的href URL是否正確形成。如何使用Selenium驗證href網址的格式是否正確?

例如http://www.domain.com/post/12/test_post

所以我想測試一個正則表達式,如:

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

雖然我知道後(塞)的名字,我不知道ID是什麼。 我該如何使用Selenium來測試href網址的格式是否正確?

我想:

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

海蘭,

看來你不關閉匹配

想盡了辦法的括號:

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