I am writing a Selenium Webdriver test using python2.7 to use XPath to select a link node set. For each link, I need to change the href attribute using driver.execute_script to execute javascript.

Trying to build the XPath string to vary the index in a separate loop.

The original statement which I need to build as a string and vary the index in a separate loop:

elem2 = driver.find_element_by_xpath("(//a[contains(text(),'Comment')])[3]") 
xp_str1 = str("\"(//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]\"")
str_elem = xp_str1 + xp_str2 + xp_str3

elem2 = driver.find_element_by_xpath(str_elem)   
driver.execute_script("arguments[0].href = 'social_media/comment/type/peer/id/9999';", elem2)

I consistently get the webdriver error:

 InvalidSelectorException: Message: u'The given selector " //a[contains(text(),\'Comment\')])[3]" is either invalid or does not result in a WebElement. The following error occurred:\nInvalidSelectorError: Unable to locate an element with the xpath expression "(//a[contains(text(),\'Comment\')])[3]" because of the following error:\n[Exception... "The expression cannot be converted to return the specified type."  code: "0" nsresult: "0x805b0034 (TypeError)"

Can any one give me ideas?

  • 1
    So your original statement works, but you're having trouble putting it into a loop? – Mark Mayo Jun 25 '13 at 0:54
  • The original statement works for each node in the set. I would like to dynamically generate the xpath string in order to vary the index (in this instance [3]) and test each member of the set. The loop shouldn't be a problem, but concatenating the xpath string and using it as a string variable with webdriver (str_elem), seems to be a problem. – anthony Jun 25 '13 at 16:41

I seems for me that you have too much quotation marks in here. I think

xp_str1 = str("//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]")
str_elem = xp_str1 + xp_str2 + xp_str3

fixes this because when you use


quotation marks mean that argument or function is a string. And when you use


the argument for function is string variable, so you don't need additional quotation marks.


The problem looks (from the error) like it may have something to do with the extra '(' in your selector. Given you state the original statement works:

elem2 = driver.find_element_by_xpath("(//a[contains(text(),'Comment')])[3]")

and this is the bad one:

The given selector "(//a[contains(text(),\'Comment\')])[3]"

you can clearly see the difference when lined up. You appear to have an extra '(' at the start.

I believe

xp_str1 = str("\"//a[contains(text(),'Comment')])[")
xp_str2 = str(3)
xp_str3 = str("]\"")
str_elem = xp_str1 + xp_str2 + xp_str3

may fix that. I just tried it quickly in an online interpreter, and it appears to fix it.

  • Thanks for the response. Using the string assignment str_elem after removing the extra '(', still gives the same error. In fact without that '(', the syntax becomes unbalanced in the final string, the opening parentheses are 2 and the closing parentheses are 3. My problem remains. – anthony Jun 26 '13 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.