【问题标题】:How to deal with xpath with apostrophe如何处理带撇号的xpath
【发布时间】:2018-12-16 15:50:59
【问题描述】:
Web 元素包含撇号,所以我不知道如何在我的 selenium 自动化脚本中创建 xpath。
脚本部分是
WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.='{0}']", **category**))).FirstOrDefault(x => x.Displayed);
变量类别将为“Sort 'Browse Publications'过滤器:”,但在运行自动化时不起作用
我尝试了转义撇号的方式,但它也不起作用。
WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.=\"{0}\"]", category))).FirstOrDefault(x => x.Displayed);
【问题讨论】:
标签:
selenium
xpath
automation
apostrophe
【解决方案1】:
试试,
String category = "Sort \'Browse Publications\' filter:";
WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.='{0}']", category))).FirstOrDefault(x => x.Displayed);
【解决方案2】:
试试这个 xPath:
//h3[contains(., 'Browse Publications') and contains(., 'filter')]
示例代码是这样的:
String category = "//h3[contains(., 'Browse Publications') and contains(., 'filter')]";
WebElement settingSection = FindElements(By.XPath(category)).FirstOrDefault(x => x.Displayed);
【解决方案3】:
尝试以下 WebElement
WebElement settingSection=driver.findElement(By.xpath("//h3[text()=\"Sort 'Browse Publications' filter:\"]"));
【解决方案4】:
找出解决办法:
WebElement settingSection = FindElements(By.XPath(string.Format(".//*[.=\"{0}\"]", category))).FirstOrDefault(x => x.Displayed);