How can I dynamically create an xpath string in Python for a webdriver xpath selector?



  • 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?



  • 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.



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2