通过id / xpath查找包含点/破折号的元素

时间:2019-08-13 16:07:23

标签: .net selenium-webdriver

无法通过XPath / ID / tagName在Firefox Web驱动程序(硒)上定位元素... 所有这些标签都包含破折号“-”,点“。”。用他们的名字。

HTML

<div class="col-xs-10 col-md-9 col-lg-8 form-field">
    <div class="sn-stream-textarea-container">
        <span class="sn-stream-input-decorator" style="background-color: gold"></span>
        <textarea id="activity-stream-work_notes-textarea" aria-label="Work notes (fulfiller only)" class="sn-string-textarea form-control ng-pristine ng-untouched ng-valid ng-isolate-scope ng-empty ng-valid-required"
            placeholder="Work notes (fulfiller only)" data-stream-text-input="work_notes" ng-required="activity_field_1.mandatory &amp;&amp; !activity_field_1.filled" ng-model="activity_field_1.value" ng-attr-placeholder="{{activity_field_1.label}}" sn-sync-with="activity_field_1.name"
            sn-sync-with-value-in-fn="reduceMentions(text)" sn-sync-with-value-out-fn="expandMentions(text)" mentio="" mentio-id="'activity-stream-work_notes-textarea'" mentio-typed-term="typedTerm" mentio-require-leading-space="true" mentio-trigger-char="'@'"
            mentio-items="members" mentio-search="searchMembersAsync(term)" mentio-template-url="/at-mentions.tpl" mentio-select="selectAtMention(item)" mentio-suppress-trailing-space="true" sn-resize-height="" style="overflow: hidden; overflow-wrap: break-word; height: 64px;"
            autocomplete="off" aria-invalid="false">
        </textarea>
    </div>
    <ul id="fieldmsg-singleinput-7" class="ng-hide" tabindex="-1" ng-show="activity_field_1.messages.length > 0" aria-hidden="true">
        <!-- ngRepeat: msg in activity_field_1.messages -->
    </ul>
</div>

代码尝试

driver.FindElement(By.XPath("//[@id='activity-stream-work_notes-textarea']")).SendKeys("Test");
driver.FindElement(By.Id("activity-stream-work_notes-textarea")).SendKeys("test");

遇到错误:

  

无法找到元素:#activity \ -stream \ -work_notes \ textarea

尽管可以接受下划线“ _”,但它还是在破折号/点之前添加了“ \”。

See screenshot of error

4 个答案:

答案 0 :(得分:0)

尝试这个xpath,

driver.FindElement(By.xpath("//textarea[contains(@aria-label,'Work notes')]")).SendKeys("test");

答案 1 :(得分:0)

当ID可用时,最好使用ID。这将起作用:

driver.FindElement(By.cssSelector(“ textarea [id ='activity-stream-work_notes-textarea']”)))。SendKeys(“ test”);

答案 2 :(得分:0)

您的xpath-driver.FindElement(By.xpath(“ // div / div / textarea”));

答案 3 :(得分:0)

该元素隐藏在2个iframe下,现在切换到正确的iframe后无法定位 谢谢你们的帮助!