Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

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.

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?

share|improve this question
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

2 Answers 2

up vote 0 down vote accepted

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.

share|improve this answer
    
Thank you very much @Alissa this works. You have got the right explanation. Problem solved! –  anthony Jul 17 '13 at 14:20

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.

share|improve this answer
    
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

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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