简体   繁体   English

如何使用硒从C#中的角度下拉列表中选择一个值

[英]How to select a value from an angular dropdown in C# with selenium

I'm trying to click a value from an angular dropdown. 我正在尝试从角度下拉列表中单击一个值。 When I click on the dropdown arrow, an option becomes available to 'Make Primary Contact' which I want to click using Selenium WebDriver and C# 当我单击下拉箭头时,我想使用Selenium WebDriver和C#单击的“成为主要联系人”选项变为可用

I have tried the following 我尝试了以下

.SendKeys(Keys.ArrowDown) .SendKeys(Keys.ArrowDown)

Also I tried locating the element by XPath but as it is dynamic, this isn't working either. 我也尝试通过XPath定位该元素,但由于它是动态的,因此也不起作用。

Here is the HTML 这是HTML

<section>
<div class="entry-box">
    <input class="inv-input" [ngModel]="value?.ItemValue" (ngModelChange)="updatePhoneNumber($event)"/>

     <div class="caret-container" (click)="toggleDropDown()">
        <span *ngIf="value?.IsPrimary">{{'contactDetailsTab.selectedPrimaryLabel' | translate}}</span>
        <img class="select-icon" src="/assets/app/caret-dark.svg">
    </div> 
</div>
<ng-container *ngIf="showDropDown">
    <div  class="dropdown-container">
        <ul #dropDown tabIndex="-1" class="dropdown" (blur)="onBlur()">
            <li *ngIf="!value?.IsPrimary" (mousedown)="setPrimary(true)">{{'contactDetailsTab.makePrimaryContact' | translate}}</li>
            <li *ngIf="value?.IsPrimary" (mousedown)="setPrimary(false)">{{'contactDetailsTab.removePrimaryContact' | translate}}</li>
        </ul>
    </div>
</ng-container>
</section>

I use the SelectElement function of Selenium: 我使用Selenium的SelectElement函数:

new SelectElement(driver.FindElement(By.Xpath("your Xpath here"))).SelectByText("Make Primary Contact");

Here you have more info about SelectElement 在这里,您可以获得有关SelectElement的更多信息。

Is the best way to deal with dropdowns and check the values inside. 是处理下拉列表并检查其中值的最佳方法。 You also can SelectByIndex or whatever is more suitable for you. 您还可以选择SelectByIndex或任何更适合您的内容。

这对我有用

driver.FindElement(By.XPath("//li[contains(., 'Make Primary Contact')]"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM