-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,559
  • 5
  • 22
  • 48
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
  • 308
  • 1
  • 4
  • 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,559
  • 5
  • 22
  • 48
0

Try with following WebElement

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

Find out a solution:

WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.=\"{0}\"]", category))).FirstOrDefault(x => x.Displayed);
undetected Selenium
  • 183,867
  • 41
  • 278
  • 352
robertredrain
  • 59
  • 1
  • 1
  • 10