-1

The web element contains apostrophe, so I do not know how to create the xpath in my selenium automation script.

The script part is

WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.='{0}']", **category**))).FirstOrDefault(x => x.Displayed);

the variable category will be "Sort 'Browse Publications' filter:", but it could not work when run the automation

enter image description here

I tried the way with escape apostrophe, but it does not work either.

enter image description here

enter image description here enter image description here

WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.=\"{0}\"]", category))).FirstOrDefault(x => x.Displayed);
Andrei Suvorkov
  • 5,399
  • 5
  • 20
  • 43
robertredrain
  • 59
  • 1
  • 1
  • 10
  • Possible duplicate of [How to use apostrophe (') in xpath while finding element using webdriver?](https://stackoverflow.com/questions/37542773/how-to-use-apostrophe-in-xpath-while-finding-element-using-webdriver) – JeffC Jul 09 '18 at 03:43
  • Please share exception details, – Ishita Shah Jul 09 '18 at 04:59

4 Answers4

0

Try,

String category = "Sort \'Browse Publications\' filter:";
WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.='{0}']", category))).FirstOrDefault(x => x.Displayed);
Magesh
  • 198
  • 1
  • 2
  • 18
0

Try this xPath:

//h3[contains(., 'Browse Publications') and contains(., 'filter')]

The sample code is somethig like this:

String category = "//h3[contains(., 'Browse Publications') and contains(., 'filter')]";
WebElement settingSection = FindElements(By.XPath(category)).FirstOrDefault(x => x.Displayed);
Andrei Suvorkov
  • 5,399
  • 5
  • 20
  • 43
0

Try with following WebElement

WebElement settingSection=driver.findElement(By.xpath("//h3[text()=\"Sort 'Browse Publications' filter:\"]"));
Shital Mokashi
  • 149
  • 1
  • 8
0

Find out a solution:

WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.=\"{0}\"]", category))).FirstOrDefault(x => x.Displayed);
undetected Selenium
  • 151,581
  • 34
  • 225
  • 281
robertredrain
  • 59
  • 1
  • 1
  • 10