如何通过 Selenium WebDriver 和 C# select 列表 (ul) 中的项目 (li) - 定位时超时
How to select an item (li) within a list (ul) through Selenium WebDriver and C# - timeout when locating
似乎无法使用 WebDriver select 来自未排序列表 <ul>
的列表项 <li>
。定位时收到超时。
HTML :
<div class="row">
<div class="col-xs-12 col-sm-6 col-lg-6">
<div class="btn btn-block btn-dropdown-white" data-ng-class="{'has-error': updateFormTemplate.gender.$invalid }" dropdown="">
<input id="hdnGender" name="gender" class="form-control ng-pristine ng-untouched ng-valid ng-isolate-scope ng-valid-maxlength" data-ng-model="updateInfo.gender" nl-input="" nl-validation-type="'gender'" nl-required="true" maxlength="1" nl-action-button="update" nl-on-enter-action="update" type="hidden">
<button type="button" name="genderButton" class="btn btn-lg form-control ng-pristine ng-valid ng-binding ng-isolate-scope ng-touched" dropdown-toggle="" data-ng-model="updateInfo.gender" nl-input="" nl-validation-hidden="hdnGender" nl-action-button="update" nl-on-enter-action="update" aria-haspopup="true" aria-expanded="false">
Ansprache <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li data-ng-click="updateInfo.gender = ''">Gender</li>
<li data-ng-click="updateInfo.gender = 'M'" class="">Male</li>
<li data-ng-click="updateInfo.gender = 'F'">Female</li>
</ul>
</div>
<div class="display-inline-block inner-top-xxs" data-ng-class="{'has-error': updateFormTemplate.gender.$invalid }">
<span data-ng-show="updateFormTemplate.gender.$invalid" class="help-block ng-hide">Please select gender</span>
</div>
</div>
</div>
我的代码:
IList <IWebElement> dropDownMenues = driver.FindElements(By.ClassName("dropdown-menu")); //Populates all drop down menues
IList<IWebElement> selectedGender = dropDownMenues[1].FindElements(By.TagName("li")); //Populate drop down items
//dropDownMenues[1].Click(); - This code doens't expand the drop down menu
driver.FindElement(By.XPath("/html/body/div[3]/div/div[6]/div/div/div/div[2]/div/div/form/div/div/div[2]/div/div[1]/button")).Click(); //This code opens the drop down
selectedGender[1].Click(); //Here's where I get the timeout
经过许多不同的变体后,我一直得到相同的结果 - 列表项未 selected。
要单击 <ul>
中的任何 <li>
项,因为这些元素是 Angular 元素,您必须为 WebDriverWait所需的 元素可点击 并点击文本为 Male 的选项,您可以使用以下解决方案:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='btn btn-lg form-control ng-pristine ng-valid ng-binding ng-isolate-scope ng-touched' and @name='genderButton']"))).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='btn btn-lg form-control ng-pristine ng-valid ng-binding ng-isolate-scope ng-touched' and @name='genderButton']/ul//li[contains(@data-ng-click, 'gender') and text()='Male']"))).Click();
这也可以通过单击两次来完成。
第一次单击 xpath:
//*[@name='genderButton']
然后第二次点击 xpath :
//*[@class='dropdown-menu']/li[2]
P.S:使用 webdriver 等待,以便您有时间加载元素。
事实证明,解决方案非常简单,而不是突然想到的...
我所要做的就是 将我的 ChromeDriver.exe 更新到最新版本(它已经 8 个月大了)- 我能够找到之前导致超时的所有元素。
不久前我在我的 Visual Studio 中更新了一些 Selenium 包,这很可能导致了这种情况,但我不记得应该相应地更新 ChromeDriver。
似乎无法使用 WebDriver select 来自未排序列表 <ul>
的列表项 <li>
。定位时收到超时。
HTML :
<div class="row">
<div class="col-xs-12 col-sm-6 col-lg-6">
<div class="btn btn-block btn-dropdown-white" data-ng-class="{'has-error': updateFormTemplate.gender.$invalid }" dropdown="">
<input id="hdnGender" name="gender" class="form-control ng-pristine ng-untouched ng-valid ng-isolate-scope ng-valid-maxlength" data-ng-model="updateInfo.gender" nl-input="" nl-validation-type="'gender'" nl-required="true" maxlength="1" nl-action-button="update" nl-on-enter-action="update" type="hidden">
<button type="button" name="genderButton" class="btn btn-lg form-control ng-pristine ng-valid ng-binding ng-isolate-scope ng-touched" dropdown-toggle="" data-ng-model="updateInfo.gender" nl-input="" nl-validation-hidden="hdnGender" nl-action-button="update" nl-on-enter-action="update" aria-haspopup="true" aria-expanded="false">
Ansprache <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li data-ng-click="updateInfo.gender = ''">Gender</li>
<li data-ng-click="updateInfo.gender = 'M'" class="">Male</li>
<li data-ng-click="updateInfo.gender = 'F'">Female</li>
</ul>
</div>
<div class="display-inline-block inner-top-xxs" data-ng-class="{'has-error': updateFormTemplate.gender.$invalid }">
<span data-ng-show="updateFormTemplate.gender.$invalid" class="help-block ng-hide">Please select gender</span>
</div>
</div>
</div>
我的代码:
IList <IWebElement> dropDownMenues = driver.FindElements(By.ClassName("dropdown-menu")); //Populates all drop down menues
IList<IWebElement> selectedGender = dropDownMenues[1].FindElements(By.TagName("li")); //Populate drop down items
//dropDownMenues[1].Click(); - This code doens't expand the drop down menu
driver.FindElement(By.XPath("/html/body/div[3]/div/div[6]/div/div/div/div[2]/div/div/form/div/div/div[2]/div/div[1]/button")).Click(); //This code opens the drop down
selectedGender[1].Click(); //Here's where I get the timeout
经过许多不同的变体后,我一直得到相同的结果 - 列表项未 selected。
要单击 <ul>
中的任何 <li>
项,因为这些元素是 Angular 元素,您必须为 WebDriverWait所需的 元素可点击 并点击文本为 Male 的选项,您可以使用以下解决方案:
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='btn btn-lg form-control ng-pristine ng-valid ng-binding ng-isolate-scope ng-touched' and @name='genderButton']"))).Click();
new WebDriverWait(driver, TimeSpan.FromSeconds(20)).Until(ExpectedConditions.ElementToBeClickable(By.XPath("//button[@class='btn btn-lg form-control ng-pristine ng-valid ng-binding ng-isolate-scope ng-touched' and @name='genderButton']/ul//li[contains(@data-ng-click, 'gender') and text()='Male']"))).Click();
这也可以通过单击两次来完成。 第一次单击 xpath:
//*[@name='genderButton']
然后第二次点击 xpath :
//*[@class='dropdown-menu']/li[2]
P.S:使用 webdriver 等待,以便您有时间加载元素。
事实证明,解决方案非常简单,而不是突然想到的... 我所要做的就是 将我的 ChromeDriver.exe 更新到最新版本(它已经 8 个月大了)- 我能够找到之前导致超时的所有元素。
不久前我在我的 Visual Studio 中更新了一些 Selenium 包,这很可能导致了这种情况,但我不记得应该相应地更新 ChromeDriver。