在driver.FindElement(By.XPath)行上获取cs1010(常量换行)异常

时间:2019-06-03 21:55:56

标签: c# selenium xpath

反斜杠和撇号都是XPath中常用的字符。也许总是会发生这种情况,而我现在才看到它,因为我向该行添加了一个监视,但是似乎每条FindElement行都抛出此异常

引发此错误的一行是...

if (loadedPage.FindElements(By.XPath("//div[@class='field signature capture']")).Count != 0)

我希望正确的XPath语法不会引起异常。

HTML ...

                    <div class='loaded page' id='page_0' style='height:1030.0px;width:796.0px;background-image:url(/packages/INDle9T1Y4PGRpKDir2KZxuTWoY=/documents/26a85bc2-d735-4135-ad15-d09ce11699f1/pages/0?v=322608);'>
                      <!-- - cssclass  = "field role_#{approval.role.id} #{field.type} #{field.subtype} #{approval.accepted? ? 'accepted' : ''}" -->
                      <div approval='hc1T31A6E0I6' class='field signature capture ' esl_id='_ircL7s98umMX' id='ircL7s98umMX' style='height:55.0px;left:468.0px;top:369.0px;width:220.0px;' tabindex='1' title='Click on the yellow box to capture your handwritten signature.'>
                        <div class='loading'>&nbsp;</div>
                        <div class='required sticky unselectable' role='presentation' style='left:-468.0px;top: 27.5px;'>&nbsp;</div>
                        <div class='content'>
                          <div class='name unselectable'>
                            <div class='cts'>
                              <img class='checkmark' src='/images/ballot_x.png'>
                              Click to Sign
                            </div>
                          </div>
                          <div class='bg required' style='height:53.0px;width:218.0px;'><span class='capture-indicator'></span></div>
                        </div>
                      </div>

2 个答案:

答案 0 :(得分:0)

要检查您的HTML,我可以看到class属性的末尾有空白。

代替这个。

if (loadedPage.FindElements(By.XPath("//div[@class='field signature capture']")).Count != 0)

尝试一下。

if (loadedPage.FindElements(By.XPath("//div[contains(@class,'field signature capture')]")).Count != 0)

OR

if (loadedPage.FindElements(By.XPath("//div[normalize-space(@class)='field signature capture']")).Count != 0)

答案 1 :(得分:0)

虽然这没有解决与FindElement的语法有关的最初问题,而是导致CS1010:常量异常中的换行符,但这确实使我摆脱了困扰我的问题。

谢谢KunduK