How to use apostrophe (') in xpath while finding element using webdriver?

后端 未结 5 510
难免孤独
难免孤独 2020-12-18 19:32

I need to use apostrophe (\') in my xpath expression which i need to use in finding elements using webdriver

i need to use below Xpath expression

//         


        
相关标签:
5条回答
  • 2020-12-18 19:46

    The Escape character usage does not serve the purpose. I tried the concatenation function and it worked like a charm. Please see the below xpath.

    tag: li Manager of Workflow Initiator's Manager /li

    Concatenate function and split the string as –

    concat('Manager of Workflow Initiator',"'",'s Manager')
    

    Single quote is kept in double quote while other characters are kept in single quotes..

    So XPath looks as –

    //li[.=concat('Manager of Workflow Initiator',"'",'s Manager')]
    
    0 讨论(0)
  • 2020-12-18 19:49

    if the above solution doesn't work then use the below solution using escape sequence.

    xpath: //li[.=\"Manager of Workflow Initiator's Manager\"]
    

    Here we are treating the whole text as a string using the escape character

    0 讨论(0)
  • 2020-12-18 19:50

    Use the xpath as shown below:

    driver.findElements(By.xpath("//input[contains(@text,\"WE'd\")]"));
    

    Hope this helps.

    0 讨论(0)
  • 2020-12-18 19:52

    You have to use double-quotes as your XPath string literal delimiter, since XPath 1.0 doesn't provide a way of escaping quotes. In addition to that, you can escape the double-quotes in Java, to avoid it from conflicting with your Java string delimiter, which also use double-quotes :

    driver.findelements(By.xpath("//input[@text=\"WE'd like to hear from you\"]"))
    
    0 讨论(0)
  • 2020-12-18 19:54

    I Encountered a similar situation where I need to write an xpath for an element shown below:

    Element:

    <img src="webwb/pzspacer.gif!!.gif" class="inactvIcon" data-ctl="["DatePicker"]" style="cursor:pointer;">
    

    I was able to grep the element using below Xpath, where I used the backslash to escape the characters [ and ".

    Xpath : //img[@data-ctl='\[\"DatePicker\"\]']

    Hope this helps.

    0 讨论(0)
提交回复
热议问题