2

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?

2
  • 1
    So your original statement works, but you're having trouble putting it into a loop?
    – Mark Mayo
    Commented Jun 25, 2013 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
    Commented Jun 25, 2013 at 16:41

2 Answers 2

2

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

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

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

driver.find_element_by_xpath(str_elem)

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

0
0

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.

1
  • 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
    Commented Jun 26, 2013 at 19:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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